Click here to Skip to main content
15,901,122 members
Home / Discussions / C#
   

C#

 
GeneralRe: OleDB Insert into MS Access Pin
Troske1-Mar-05 12:29
Troske1-Mar-05 12:29 
GeneralProcess.HasExited problems Pin
3Dizard1-Mar-05 9:44
3Dizard1-Mar-05 9:44 
GeneralRe: Process.HasExited problems Pin
Dave Kreskowiak1-Mar-05 10:14
mveDave Kreskowiak1-Mar-05 10:14 
GeneralRe: Process.HasExited problems Pin
3Dizard2-Mar-05 23:10
3Dizard2-Mar-05 23:10 
GeneralRe: Process.HasExited problems Pin
3Dizard3-Mar-05 0:14
3Dizard3-Mar-05 0:14 
GeneralBasic Thread Questions Pin
GhostsOfOrion1-Mar-05 8:29
GhostsOfOrion1-Mar-05 8:29 
GeneralRe: Basic Thread Questions Pin
S. Senthil Kumar1-Mar-05 8:58
S. Senthil Kumar1-Mar-05 8:58 
GeneralRe: Basic Thread Questions (LONG) Pin
GhostsOfOrion1-Mar-05 16:00
GhostsOfOrion1-Mar-05 16:00 
Thanks for your reply it is much appreciated and man was it fast! It is taking me more time to digest than it took you to write that's for sure.

It looks like I have been miss-lead about some things. Your reply has already cleared up many of them but, alas, it has raised some other questions that I hope you will be so kind to help me with.

Forms don't have threads, applications do. All controls have Window procedures which receive messages from the OS. You need (atleast) a thread to act as a message pump, to receive the messages and process them. You can create as many threads as you want. When you call Application.Run from a particular thread, that thread becomes the message pump for that form.

This was a big one that cleared up a whole bunch of confusion (as you can imagine). So, just to make sure I get you right… If I (in VS.NET) create a: File->New->Project->C#->Windows Application I only have one thread. No matter how many new forms I make, I still only have one thread (the Main Thread) and all the controls on them are just passing messages to the (hidden) Windows Message Pump. The pump then sends those events, etc. to the respective handlers in the form's code.

No, Invoke() and BeginInvoke() are needed if you are modifying the UI from a different thread. One of the basic laws of Windows is that you can modify the UI only from the thread that created it. Invoke() and BeginInvoke() are used to execute code on the thread that created the UI. It has nothing to do with normal variables. Each thread has its own stack, so local variables are not shared across threads, maybe your problem is because of this?

This really helped too. Here is a question that would help even more with this. When an Asynchronous Callback "calls back" it calls back to the thread that starts it right? Even if the class that starts it was "newed" from a different form than the form being "newed" in the Application.Run? Man I hope that makes sense… I wish I new enough to ask the questions correctly. Maybe if I ask it like this… I new the first form of the app with Application.Run then that form news another form. In that second form I press a button that starts an AsyncCallback (from a socket). When that callback calls back it calls back to the (one and only) main thread. Or is it in a new thread?

No. If it is a reference type (the Form object is), only the reference gets passed around, the whole object is not copied. It's like passing a pointer in C/C++. The ref keyword is used to achieve the effect of passing a pointer to a pointer in C/C++ (when you need to change the reference itself).

Ok, I get this... thanks for clearing that up for me.

If all they do is subscribe to events from the mainform, you can pass the mainform as part of the EventArgs.

Aha... that will come in handy! Thanks again!

A static object would solve the passing around of the main form.

It is very cool to hear you say that and I have a hunch that it's because I don't fully understand proper application design. (I am learning as much as I can about this now.) My current method has been to create a main class (not Main()) with a bunch of sub-classes that it uses. My main class acts as sort of a hub and uses the other classes to perform the details of its "high-level" work. This method has served me well and I thought I was cooking along pretty good... until I ran into threads and these AsyncCallbacks.

I was using this "main-class" with "sub-classes" method so that they could access the main class's properties. They could also access other sub-class's properties through the main class reference that they now had local because I passed my main class into their constructor and set a private MyMainClass property in them. Is this reasonable? Or are you laughing right now at my stupidity? Lol Here is some psudo code to see what I've been doing so you can set me straight.

<br />
public class MyMainClass {<br />
      public SubClass1 sc1;<br />
      public SubClass2 sc2;<br />
<br />
      public MyMainClass() {<br />
            sc1 = new SubClass1(this);<br />
            sc2 = new SubClass2(this);<br />
      }<br />
}<br />
<br />
public class SubClass1 {<br />
      private MyMainClass _mmc;<br />
      public somevar<br />
<br />
      public SubClass1(MyMainClass mmc) {<br />
            _mmc = mmc;<br />
      }<br />
}<br />
<br />
public class SubClass2 {<br />
      private MyMainClass _mmc;<br />
<br />
      public SubClass2(MyMainClass mmc) {<br />
            _mmc = mmc;<br />
            If (_mmc.sc1.somevar == something) {<br />
                  Do something important;<br />
            }<br />
     }<br />
}<br />


As my apps got bigger I found it really ugly to be passing my main class into each of my sub-classes. So I had the bright idea (maybe not so bright) of putting a "using" include at the top of each of my sub-classes that looked like this:

using mc = MyNamespace.MyMainClass;

Now I could just access the main class without passing it through the constructor. But, as I'm learning, it looks to me like that is only pointing the MyMainClass on the stack not the heap. I'm so damned confused. I hope this isn't as stupid as I feel it could be. Is this a bad way to go about it? How would you tackle this? Or should I just scrap this thinking altogether?

I am reading and learning as much as I possibly can but I don't know anyone who I can ask questions. I am very eager and grateful for your help.

Reguards,


EB
GeneralRe: Basic Thread Questions (LONG) Pin
S. Senthil Kumar1-Mar-05 17:04
S. Senthil Kumar1-Mar-05 17:04 
GeneralRe: Basic Thread Questions (LONG) Pin
GhostsOfOrion2-Mar-05 4:17
GhostsOfOrion2-Mar-05 4:17 
GeneralInherited Forms in Compact Framework Pin
Tristan Rhodes1-Mar-05 7:59
Tristan Rhodes1-Mar-05 7:59 
GeneralOverriding Text in UserControl :confused: Pin
_eulogy_1-Mar-05 7:56
_eulogy_1-Mar-05 7:56 
GeneralRe: Overriding Text in UserControl :confused: Pin
spif20011-Mar-05 20:20
spif20011-Mar-05 20:20 
GeneralRe: Overriding Text in UserControl :confused: Pin
_eulogy_2-Mar-05 0:33
_eulogy_2-Mar-05 0:33 
GeneralForm Designer for Lightweight Framework Pin
Tristan Rhodes1-Mar-05 7:32
Tristan Rhodes1-Mar-05 7:32 
Generalupdating already opened window/form Pin
Adnan Siddiqi1-Mar-05 7:09
Adnan Siddiqi1-Mar-05 7:09 
GeneralExporting Crystal Reports To PDF Errors Pin
Khang Nguyen1-Mar-05 7:03
Khang Nguyen1-Mar-05 7:03 
GeneralRe: Exporting Crystal Reports To PDF Errors Pin
Raganar1-Mar-05 7:59
Raganar1-Mar-05 7:59 
GeneralRe: Exporting Crystal Reports To PDF Errors Pin
Khang Nguyen1-Mar-05 8:24
Khang Nguyen1-Mar-05 8:24 
GeneralAttributes property Pin
MyThread1-Mar-05 6:19
MyThread1-Mar-05 6:19 
GeneralRe: Attributes property Pin
Robert Rohde1-Mar-05 6:51
Robert Rohde1-Mar-05 6:51 
GeneralRe: Attributes property Pin
MyThread1-Mar-05 6:58
MyThread1-Mar-05 6:58 
GeneralForms in DLL's in .Net Lightweight Pin
Tristan Rhodes1-Mar-05 5:59
Tristan Rhodes1-Mar-05 5:59 
GeneralUpdate Pin
Tristan Rhodes1-Mar-05 6:03
Tristan Rhodes1-Mar-05 6:03 
GeneralMore stuff that cause LightweightFramework to crash. Pin
Tristan Rhodes1-Mar-05 6:39
Tristan Rhodes1-Mar-05 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.