AutoResetEvent..WaitOne() Question

G

Guest

I have an application that has a main thread and a worker thread.
From time to time, the main thread, because of user input, needs to notify
the worker thread to execute an extra bit of code, in addition to it's normal
work load.

I created 5 AutoResetEvent objects and have the main thread call set when it
wants the worker thread to execute some additional code. In the worker
thread, I have 5 separate WaitOne(0,false) functions being called; one for
each AutoResetEvent object . It was my hope that the worker thread would do
a quick check (no wait time) to see if the event was set and then proceed
with it's other work.

Unfortunately, there appears to be some overhead with this WaitOne()
function, because my CPU usage is running at about 70% when I have these 5
WaitOne() functions in the worker thread. However, if I comment them out,
the CPU usage drops to about 15%.

Does anyone know why this is such a drain on the system? Am I misusing
these Events? Is there a better or more efficient way to signal between
threads?

Thanks for your help.

Phil
 
N

Nicholas Paldino [.NET/C# MVP]

Phil,

I wonder why you would use a separate thread to do these operations.
Why not just put all of your work into thread pool requests (or at least
some of them)? You say yourself that you have an extra bit of code that
needs to be executed, which makes it a perfect candidate for a thread pool
operation.

Hope this helps.
 
G

Guest

The worker thread is reading from and writing to a com port. Ordinarily, it
would simply read from the com port, but there are occasions where the user
may need to send data back down to the device connected to the com port. So
I use Events to tell the thread to stop reading from the com port, send down
this data, and then resume reading from the com port. A special thread using
Events seems like the best solution for this scenario. Do you disagree?

Phil



Nicholas Paldino said:
Phil,

I wonder why you would use a separate thread to do these operations.
Why not just put all of your work into thread pool requests (or at least
some of them)? You say yourself that you have an extra bit of code that
needs to be executed, which makes it a perfect candidate for a thread pool
operation.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Phil said:
I have an application that has a main thread and a worker thread.
From time to time, the main thread, because of user input, needs to notify
the worker thread to execute an extra bit of code, in addition to it's
normal
work load.

I created 5 AutoResetEvent objects and have the main thread call set when
it
wants the worker thread to execute some additional code. In the worker
thread, I have 5 separate WaitOne(0,false) functions being called; one for
each AutoResetEvent object . It was my hope that the worker thread would
do
a quick check (no wait time) to see if the event was set and then proceed
with it's other work.

Unfortunately, there appears to be some overhead with this WaitOne()
function, because my CPU usage is running at about 70% when I have these 5
WaitOne() functions in the worker thread. However, if I comment them out,
the CPU usage drops to about 15%.

Does anyone know why this is such a drain on the system? Am I misusing
these Events? Is there a better or more efficient way to signal between
threads?

Thanks for your help.

Phil
 

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