Accessing the ManualResetEvent of a thread

B

Brian Mitchell

I am sure this is pretty basic but I have never worked with ResetEvents
before, but how do I signal a ManualResetEvent of a module that is spun off
into several threads? For instance, if I have 5 threads of the same Sub and
they are all unsignaled, how would I signal them (or one of them)?

Here is a sample of my problem...

Thanks!!

Imports System.Threading
Module Module1

Private mre As ManualResetEvent

Sub Main()

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Thread), 1)
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Thread), 2)
Console.WriteLine("Waiting")
Console.ReadLine()

End Sub

Private Sub Thread(ByVal State As Object)

mre = New ManualResetEvent(False)
'...some code
mre.WaitOne()

Console.WriteLine("Done with: " & State)

End Sub

End Module
 
B

Brian Mitchell

I created an array to help out for right now, but is this the correct way
for referencing the ResetEvents? I want to make sure I am doing this
correctly and not just finding a "work around"

Thanks.

My new declaration now looks like this...

Private mre(10) As ManualResetEvent

(Also how do I dimension this? I used 10 in my sample program but in the
real one there could be as little as 3 or as many as 25. There will be a
completely random number of threads and I want to make sure I am not under
or oversizing the mre()).
 
J

Jay B. Harlow [MVP - Outlook]

Brian,
Private mre(10) As ManualResetEvent
It appears you are passing the "index" of the Thread as the State parameter
(the 1 & 2 on the above calls).

You can simply use this "index" to index into the array:
Dim index As Integer = DirectCast(state, Integer)

mre = mre(index)

Rather then pass the index as the State, I normally pass the
ManualResetEvent itself or other object that acts as the parameter to the
thread event.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top