Click here to Skip to main content
15,918,808 members
Home / Discussions / C#
   

C#

 
GeneralRe: OleDB Insert into MS Access Pin
Anonymous1-Mar-05 10:42
Anonymous1-Mar-05 10:42 
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 
From what I understand so far each Form runs in its own thread and each of its controls run on their own threads. I wish that we were in person so that you could stop me right here if I'm wrong but I will continue with my questions based on that premise which leads me to my first question.

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.

1. When the Application.Run() method (in the main entry point for the application.) runs the Form passed to it, is there now 2 threads running? The App thread and the Form's thread? Or does the Form's thread become the MAIN THREAD?

Application.Run starts a message loop on the current thread. So if you call Application.Run from Main(), the main thread is used as the message pumping thread. You can see that for yourself, Application.Run() blocks and doesn't return (until the app closes).


Either way there is a Main Thread. So, when other threads are run from the main thread they need to Invoke() or BeginInvoke() back to the main thread, as they need to change variables in the app's main thread or else the change only seems to occur in the context of the thread making the changes.

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?

2. Is it normal to have a large amount of EventHandlers in the main thread (main form?) for the other threads to invoke back to?...

That's ok as long as it makes sense in that context. Again, you are passing a Form object, not a thread object.

3. When you pass an object into another (or a method I would presume) does that object get copied into a new area in memory?

I know that there is a ref keyword that only sends a reference to the same memory...

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).


4. If passing my main-thread-form into each of my 40 class's constructor (as shown above) does that mean that I now have 41 main-thread-form-objects in memory? If so, wouldn't it be better to:
Form2 frm2 = new Form2(ref this);

...and use the reference to invoke off of?

Well, there will be only object in memory and 41 references to it. And the code you wrote won't compile, you can't pass "this" by ref as that would mean that you would be able to change "this" itself.


5. If passing the main-thread-form into every other class that needs to talk with it isn't the right way to do this then what is?

What do you do inside those 500 classes? If all they do is subscribe to events from the mainform, you can pass the mainform as part of the EventArgs.


6. Is the answer some kind of static object which contains the instance of the object?

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

Regards
Senthil
My Blog
GeneralRe: Basic Thread Questions (LONG) Pin
GhostsOfOrion1-Mar-05 16:00
GhostsOfOrion1-Mar-05 16:00 
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 

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.