Multi-Project Solution Help

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 4217 Views - Last Post: 11 January 2011 - 06:38 PM Rate Topic: -----

#1 darkjohn20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-June 10

Multi-Project Solution Help

Posted 09 January 2011 - 02:05 PM

Alright, so I have a Console project and a Windows Forms project. At the moment the Forms project is a reference in the Console project.

My first question is:
Is there any way to have variables that are global to the solution, and not just the project? I know how to set up globals within the project, but not across projects.

I need information gathered in the Console to be used in the Windows Form. If variables can't actually be global, but can be passed across projects through some means, such as a function, that works, too. Otherwise, what would be the best possible solution for having a Console and Windows Form interact? I, ideally, want them to have some shared references and variables.

If any other info is needed I will provide it.

Is This A Good Question/Topic? 0
  • +

Replies To: Multi-Project Solution Help

#2 Amrykid   User is offline

  • 4+1=Moo
  • member icon

Reputation: 150
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Multi-Project Solution Help

Posted 09 January 2011 - 02:23 PM

View Postdarkjohn20, on 09 January 2011 - 02:05 PM, said:

Alright, so I have a Console project and a Windows Forms project. At the moment the Forms project is a reference in the Console project.

My first question is:
Is there any way to have variables that are global to the solution, and not just the project? I know how to set up globals within the project, but not across projects.

I need information gathered in the Console to be used in the Windows Form. If variables can't actually be global, but can be passed across projects through some means, such as a function, that works, too. Otherwise, what would be the best possible solution for having a Console and Windows Form interact? I, ideally, want them to have some shared references and variables.

If any other info is needed I will provide it.



Try looking up IPC (Inter-process communication). Click here.
Was This Post Helpful? 2
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 08:00 AM

I think that depends on what kind of information you need shared, and in what context? Realtime? As in, when I type something into the console, it's updated in the windows forms? Thats probably going to require some interprocess communication.

If it's just gathering non-user inputted information, I'd change the console app to a class library, and make a class designed to gather and store this information. Then you can just use that class in your other project.
Was This Post Helpful? 2
  • +
  • -

#4 darkjohn20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 01:08 PM

My goal is to have the user type something, and based on what they type (which is determined/parsed in the Console) send certain data to the Form.

After looking at some methods of IPC:

Quote

Clipboard
COM
Data Copy
DDE
File Mapping
Mailslots
Pipes
RPC
Windows Sockets


I have decided that, if there is no easier method than these, sockets are the easiest for me to do.

Thanks for the help. I hadn't even realized that separate Projects in a Solution would be different executables, otherwise I would have quickly jumped to sockets.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Multi-Project Solution Help

Posted 10 January 2011 - 01:17 PM

Different projects in a solution need NOT be different executables. As mentioned, you could have many class libraries/assemblies in a project and a single executable that used these.
Was This Post Helpful? 1
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 01:26 PM

Well, if you're determined to have communication between processes, I suggest sockets, or at least to avoid the clipboard. I'd be pissed off if a process hijacked my clipboard data, especially if I was in the process of using it!

However, a much better solution is to use a single project, a console application (or two projects, a console app and a user control library). Add a reference to System.Windows.Forms, then add a form to the project. Build the form the way you want it. Then, in the console application, you can create an instance of that form, and .Show() it.

You can actually do this on separate threads, if you'd like. That way, the console and GUI can behave independently. This isn't all that hard to do, and it's much simpler than doing communication between processes.

This post has been edited by insertAlias: 10 January 2011 - 01:27 PM

Was This Post Helpful? 2
  • +
  • -

#7 darkjohn20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 01:44 PM

Wow, I completely forgot that I could add forms to a project, which is kind of sad considering that I've done it before. Must just be that mental block from coding all day. Thank you all for the tips. I'll come back if I have any more problems, though not before thinking about them!
Was This Post Helpful? 0
  • +
  • -

#8 darkjohn20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 02:44 PM

Forgive me, for I do not see an edit button, but I had another question.

The overall layout of my project will be a Console, Networking on another thread, and multiple forms on yet another thread.

Based on console input, both the "networker" and the forms will be started up, and my plan is to have the console still available, hence the two spawned threads.

I understand how to create a thread for the network function, but I'm not sure what to do for the forms.

Code for Network Thread, referencing file Networking.cs:
Networking Networker = new Networking();
Thread NetworkThread = new Thread(new ThreadStart(Networker.NetThread));
NetworkThread.Start();


For the form (I have one for now but am probably going to use multiple), referencing file ServerLog.cs, I have:
ServerLog Log = new ServerLog();
Log.Show();


However, at the moment, this is in my Console code, which causes it to continuously show the loading cursor over the form (because of it using the same thread?). How would I put this in its own thread, along with other forms?

I also noticed that I can't reference a control on the form using something like
Log.hEditBox


Am I forgetting to add something?

Thanks again, and in advance.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 02:50 PM

You shouldn't directly reference controls. Make public properties to obfuscate controls. Google C# properties if you don't know what I mean, or check out my tutorial on passing values between forms.

I'd probably use a BackgroundWorker to make the threading simpler.
Was This Post Helpful? 0
  • +
  • -

#10 darkjohn20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 02:58 PM

Is the threading that much more difficult for forms than other stuff? I'd like to use threads unless it's significantly harder.

I've searched for the benefits and such of Thread vs BackgroundWorker, but couldn't find much. Could you elaborate?

Also, before I get any farther, is my method of a Console with two Threads the best option if I want them all to be working at the same time? I'd rather not have to change my whole project because I didn't find a more efficient way earlier.
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 03:10 PM

I have no idea if it's the "best" option, considering I don't know what you're doing. It's very likely not the way I would do it, because I wouldn't use the console if I needed forms. I'd make another "data entry" type form and allow the user to enter data there. I'd make the whole thing a forms app, or a WPF app.

But I'm sure you have a reason. Like I said I don't know what you're making.

Using Thread objects vs. BackgroundWorker objects is a bit of a stylistic question. I like the background worker, because it's easy to use and exposes nice events. But you may like threads. Here's a good comment on stack overflow explaining the benefits of both:

http://stackoverflow...1507337#1507337
Was This Post Helpful? 0
  • +
  • -

#12 darkjohn20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 03:24 PM

My initial idea was to use a form that looked like a console, but I kind of like the way input is entered on the current line, rather than an inputbox on the bottom of the form for example. Also, when using a form with a multi-line inputbox for output and a single-line inputbox for input, the control edges either overlap (the little white edge that turns blue when focus is on the control) or don't touch. If there is a way to create a nice looking form that resembles a console I would gladly use that!

Not knowing much about BackgroundThread vs ForegroundThread, I would say I would need Foreground, so I will probably use Threads, rather than the worker.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Multi-Project Solution Help

Posted 10 January 2011 - 03:39 PM

For an example of a multi-project solution, have look at this one, (vb.net).
You can see how the separation of concerns helps.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 03:43 PM

View Postdarkjohn20, on 10 January 2011 - 03:24 PM, said:

Not knowing much about BackgroundThread vs ForegroundThread, I would say I would need Foreground, so I will probably use Threads, rather than the worker.



You've got this very confused. A Thread object is not a background or foreground thread. It's just a thread. BackgroundWorker is just a name. It, too, starts another thread. It's considered "background" because, from the perspective of the thread that kicked it off, it's in the background, not blocking. But they're both of equal priority.

Same for Thread objects. They're "background" in the sense that they don't block the main thread they were created on.

So there's no such thing as background threads or foreground threads. They're all just threads. BackgroundWorker is just an easier way to do threading. But you can do it however you like. Read the Stack Overflow link I provided so you understand the difference between these two objects.
Was This Post Helpful? 1
  • +
  • -

#15 darkjohn20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-June 10

Re: Multi-Project Solution Help

Posted 10 January 2011 - 03:50 PM

http://en.csharp-onl...kground_Threads

Perhaps I read the definition of BackgroundThread wrong. This led me to believe that they might not work as well.

Quote

can be ignored at any point in time (even if they are currently laboring over some unit of work)


I'll take a look at BackgroundWorker. As for the "mock console" form, any ideas? Not possible? I suppose the look/function of the console is just preference, so I may just use forms.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2