473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Accessing the ManualResetEvent of a thread

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
Nov 21 '05 #1
2 2800
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()).

"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:eC**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #2
Brian,
Private mre(10) As ManualResetEvent
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Thread), 1)
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Thread), 2) 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:
Private Sub Thread(ByVal State As Object)
Dim index As Integer = DirectCast(state, Integer)

mre = mre(index)
'...some code
mre.WaitOne()

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

End Sub


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

"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:en**************@TK2MSFTNGP09.phx.gbl...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()).

"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:eC**************@TK2MSFTNGP10.phx.gbl...
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


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kovan Akrei | last post by:
Hi, I would love to know if it is possible some how to ensure that a thread T1 does not call T2.myResetEvent.Set() before T1 has blocked by calling T1.myResetEvent.WaitOne()? I cant use monitors...
1
by: Pujo Aji | last post by:
Hello, I use ManualResetEvent to stop and allow a thread running by setting: public static ManualResetEvent mre = new ManualResetEvent(false); So if I use mre.WaitOne(); this let the thread...
0
by: Mark | last post by:
Hi, How do I stop a ManualResetEvent.WaitOne from intermittently jumping to 0? I am using version 1.1 of the framework. My ManualResetEvent instance is named "bev". To make the WaitOne work...
3
by: Elhurzen | last post by:
X-No-Archive: Yes >From what I understand, if multiple threads are waiting on a ManualResetEvent after calling WaitOne(), only one thread is guaranteed to be signaled when Set() is called on that...
0
by: Tomasz Kaszuba | last post by:
Is it possible to use the ManualResetEvent in my ASP.NET application? When I try to use it Asp.Net ignores the call to ManualResetEvent.WaitOne() (which should hold the thread indefinetly) and...
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
15
by: Peter Larsen [] | last post by:
Hi, I have a problem using a ManuelResetEvent in the GUI thread while receiving events from the Shell (new folders, rename etc). This is what i do: Receiving an event from the Shell....
9
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
7
by: =?Utf-8?B?cmJEZXZlbG9wZXI=?= | last post by:
The following is from a simple Windows application in VS2005, which has button1 and textbox1 dragged onto a form. In StartThreads(), I call ThreadPool.QueueUserWorkItem(), then call WaitOne()....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.