|
Question : VB.NET Threading and sending data/objects between threads?
|
|
Having the following example-class...
Class MyThread 'Dim sharedvar as string 'Global var to be updated? Sub main() Dim t As New Thread(AddressOf hiho) t.Start() End Sub
Sub hiho() Dim gotdata as string 'gotdata=thedatafrom main() Do While True Thread.Sleep(100) Console.WriteLine("hi - " & gotdata) Loop End Sub End Class
Is it possible to send in an object from the function main to the called function in the new thread (hiho)? Is it possible to send data both ways(sharing the data)? Ie by both populating the same global var?
I want to set up a main thread (the one being run when the project start) that acts like a monitor. It will start up ie 10 new threads that does exactly the same thing. Can I i send data from the main thread to the sub-threads, and if so how?. Can I send data from the subthreads to the main thread, and if so how?.
I have done some threading, but using Java (with WSAD). There I could call methods on the main thread from the subthreads.
|
|
Answer : VB.NET Threading and sending data/objects between threads?
|
|
You can't really send data back and forth like you would with passing on function arguments normally within thread. At least not that I know of. Whats normally done is instantiating a "Shared" variable class object that you either create by yourself or just an overrided variable/arraycollection that you could use.
The shared object is "shared" via passing on the reference to the main thread as well as any other sub thread that you will be creating. When you are manipulating the object, since it is referenced by all the other threads, you would need to synchronize the shared object between all the threads created so no data is lost.
An example of passing on this reference would be something like..
sub main() Dim thisObject() as New ArrayList Dim Producer as new Producer(thisObject); Dim Consumer as new Consumer(thisObject);
. . . Dim mainThread as new Thread(new ThreadStart(Producer.Produce)) Dim subThread as new Thread(new ThreadStart(Consumer.Consume))
mainThread.Start() subThread.Start() end sub
Public Class Producer Public Sub New(ByRef thiscollect As ArrayList) MyBase.New() ourArray = thiscollect End Sub
Private ourArray As ArrayList
Public Sub produce() 'Do some stuff, then use the ourArray ourArray.Add("Item1") End Sub End Class
Public Class Consumer Public Sub New(ByRef thisCollect As ArrayList) MyBase.New() ourArray = thisCollect End Sub
Private ourArray As ArrayList
Public Sub Consume() ourArray.Remove("Item1") End Sub End Class
After you pass in this reference, you could manipulate the object either in Producer or Consumer, and the object would change as it goes. The problem comes with what if both were to access the shared object at the same time ? Since they are both independent threads, it is very possible that it'd happen. To handle this, you would use the Monitor class inside the shared object. Normal arrayList class would not have the Monitor stuff necesasry to synchronize the object between each of the functions.
Basically, you want to place a "SyncLock" on the shared object before you play with it. An example would be...
Public Sub SampleThread() Dim icounter As Integer = 0 SyncLock ourArray While counter < 1000 'if the ArrayList is being accessed by someone else, we'd wait for it right here Monitor.Wait(ourArray) 'Push one element. ourArray.Add(counter) 'Release the waiting thread. Monitor.Pulse(ourArray) counter += 1 End While End SyncLock End Sub 'FirstThread
Anyway, having this should get you started I hope. You will want to start a main thread with the shared object that does an conditional infinite loop to listen for incoming information like a TCP/IP port. Then, sub threads would be started at that time as well (or added as demand increases). As soon as information is retrieved, it is stored into the shared object and every single sub threads would fight to process that one piece of information. Or you could assign an unique number to each sub thread that'd process a specific slot in an ArrayList if there're data in the slot. It all depends on how you want to implement it.
Hope that helps!
|
|
|
|