View Single Post
  #8 (permalink)  
Old April 15th, 2007, 07:19 PM
planoie's Avatar
planoie planoie is offline
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

1. Because of the way they constructed the BeginInvoke method, you can pass it anything you wish. The second argument is simply an object array so you can create an array of values you need to pass to the method that will be called:

   Me.BeginInvoke(New CounterUpdateDelegate(AddressOf UpdateCounter1), New Object() {value1, value2, value3, ...})

Alternatively, and in many cases easier to deal with, create a class that will contain all the "arguments". This can be used in much the same way as .NET uses event args classes. Your class would have properties for all the values you need to pass to the other thread, and you pass an instance of the object as a single argument to the BeginInvoke call. Of course, by doing this you have to consider thread safety.

   Me.BeginInvoke(New CounterUpdateDelegate(AddressOf UpdateCounter1), New Object() {myFormUpdateArguments})


2. The method delegate called by the BeginInvoke call can update all controls as needed.

3. I'm not really sure.

4. There's no problem here. Yes, you can not update controls created by another thread, but there's nothing preventing you from READing values from another thread's controls.


One misconception that I'm still fighting is that different threads are in different scopes of a program. I'm not sure why I have had this idea in my head but I do. It's important to remember that several threads created by the same applications are all still within the scope of the same application. It's just that another *process* is handling the execution of some number of method calls. The point here is that secondary threads within a given program can see everything that the main thread sees (according to standard scope visibility of course). Granted, some rules need to be adhered to, such as simultaneous accessing of certain resources (like files) and restrictions on what can be updated (as we've been discussing) but otherwise, the code runs just the same as if you called it from the single main thread.

-Peter
Reply With Quote