473,396 Members | 1,666 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,396 software developers and data experts.

AutoResetEvent..WaitOne() Question

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
Nov 16 '05 #1
2 12520
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]
- mv*@spam.guard.caspershouse.com

"Phil" <Ph**@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
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

Nov 16 '05 #2
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Phil" <Ph**@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
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


Nov 16 '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...
0
by: donotreplymeonthisemail | last post by:
what does WaitOne(-1, true) do with exitContext set to true? i have not been able to understand this correctly. can i use this inside lock statement?
4
by: Charles Law | last post by:
I've been using monitors a bit lately (some of you may have heard ;-) ) and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing. Are they...
1
by: Charles Law | last post by:
I have a frustrating problem where WaitOne does not return when I expect. I am using an AutoResetEvent with an overlapped structure in a call to WaitCommEvent. The data I am expecting on the...
3
by: Ed Sutton | last post by:
Can any one please offer any theories on how ManualResetEvent.WaitOne can hang when used with a timeout? When I notice my USB/Serial devices are no longer receiving communications, I hit break...
2
by: archana | last post by:
Hi all, I am not clear regarding waitone of manualresetevent. I am invoking webrequest asynchronously with say 5 requests at a time. What i am doing is after invoking request i am calling...
5
by: Maqsood Ahmed | last post by:
Hello, We have been experiencing a problem with AutoResentEvent class for past 2-3 months. It seems that it just stops at WaitOne and the thread doesn't released from blocking state. We have...
3
by: Morgan Cheng | last post by:
In order to arrange a time-out operation, I make the task running in a worker thread; and wait in main thread with AutoResetEvent help. The code is like below. AutoResetEvent ev = new...
4
by: buu | last post by:
so, I have a private object as system.threading.AutoResetEvent, and I would like to read it's current status. currently I have an another boolean object wich I update together with an...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.