Bernie Yaeger said:
Here's the real problem: I am trying to call a method/sub that has
arguments. I begin to understand that a thread is its own world, has
its own heap, doesn't even recognize global variables from another
thread, etc.
A thread is just a sequence of executed statements. Apart from the stack
containig local variables, data is not thread specific.
Is there a way to use invoke to overcome this? If I can
use invoke to launch a performclick - which has arguments - then I
should be able to use it to do what I want. Can you give me a simple
example of using invoke?
You probably know that windows applications are message driven. The OS sends
the messages, like mouse movements and pressed keys, to a window by putting
the messages in a message queue. Any thread that created a window is a UI
thread and has a message queue. A thread gets the messages only for those
windows that have been created in the same thread. The thread must process
the messages and send them to the window they belong to. This message loop
is contained in the method Application.Run, also in Form.ShowDialog. Usually
an application has only one UI thread. Application.Run is usually called in
Sub Main.
Now, a basic rule is that only the thread creating a window can access it.
The Invoke method (or BeginInvoke) can be compared to sending a message to
the window in the other thread. Using the Framework you can do a little bit
more: You can specify a procedure that is to be called in the other thread.
You can also pass arguments to the procedure.
If there's a reference to the button available in your second thread, you
can call the button's Invoke method and specify the method to be called:
Button1.Invoke(New MethodInvoker(AddressOf Button1.PerformClick))