473,804 Members | 2,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to read status of AutoResetEvent?

buu
so, I have a private object as system.threadin g.AutoResetEven t, and I would
like to read it's current status.

currently I have an another boolean object wich I update together with an
AutoResetEvent, but I don't think it's the best practice....

Feb 29 '08 #1
4 5933
buu wrote:
so, I have a private object as system.threadin g.AutoResetEven t, and I would
like to read it's current status.
You can't, for a good reason: it couldn't possibly be made thread-safe.
currently I have an another boolean object wich I update together with an
AutoResetEvent, but I don't think it's the best practice....
Actually, it is, but only if you use locks to make this access thread-safe.

In many cases, using a monitor is more flexible and easier to get right than
using an AutoResetEvent. You can use Monitor.Pulse() and Monitor.Wait() to
let threads notify each other in much the same way.

bool conditionMet;
object conditionMonito r = new object();

lock (conditionMonit or ) {
if (!conditionMet) Monitor.Wait(co nditionMonitor) ;
// conditionMet == true and no other thread is in this monitor
}

....

lock (conditionMonit or) {
conditionMet = true;
Monitor.Pulse(c onditionMonitor );
}

--
J.
Feb 29 '08 #2
so, I have a private object as system.threadin g.AutoResetEven t, and I would
like to read it's current status.
Couldn't you do a WaitOne on it for 1 millisecond?
Feb 29 '08 #3
AMercer wrote:
>so, I have a private object as system.threadin g.AutoResetEven t, and I would
like to read it's current status.

Couldn't you do a WaitOne on it for 1 millisecond?
This is a bad idea. First, it changes the state of the event if it's
signaled. You can set the event again, of course, but only if you take care
not to wait for it immediately again afterwards to "check" the status. In
the worst case you'd have to take over the responsibilitie s of the other
waiting threads.

Second, if the event is not signaled, waiting is likely to invoke a context
switch, which will probably take more than 1 millisecond, so this technique
doesn't scale. In fact, it's bizarrely more costly than checking a
self-maintained boolean, probably even if you factor in locking costs.

--
J.
Feb 29 '08 #4
Couldn't you do a WaitOne on it for 1 millisecond?
>
This is a bad idea. First, it changes the state of the event if it's
signaled. You can set the event again, of course, but only if you take care
not to wait for it immediately again afterwards to "check" the status. In
the worst case you'd have to take over the responsibilitie s of the other
waiting threads.

Second, if the event is not signaled, waiting is likely to invoke a context
switch, which will probably take more than 1 millisecond, so this technique
doesn't scale. In fact, it's bizarrely more costly than checking a
self-maintained boolean, probably even if you factor in locking costs.
So, it all depends on how often the OP wants to read it's current status.
Using WaitOne less frequently than, say, once a minute should pose no
problems. Using it more than a few times a second will be a problem. I
wonder what the OP had in mind?

Mar 1 '08 #5

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

Similar topics

1
23462
by: Bleedledeep | last post by:
I am new to C#/.NET/DirectX, and could not find a simple example of how to read the X/Y location of the joystick. < YIKES!!! The DirectX.DirectInput is one serious pile of API! > After some futzing around, I came up with the following - which seems to work just fine. I'm looking for a sanity check here, is this a good way to access the joystick? Yeah, yeah, I only look for the first joystick, and there is some hardcoded silliness in...
2
12558
by: Phil | last post by:
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...
4
3198
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 interchangeable, or when should I use one over the other? TIA Charles
0
1153
by: dotnetnoob | last post by:
hello, let's say i have two xml file 1. myData1.xml <?xml version="1.0" encoding="UTF-8"?> <myRoot> <myDataInput Number="1" Type="0" Status="Off" ObjName="test"> <Properties Attr="0" Number="1" ObjName="test"> <Description/> </Properties>
0
679
by: Arno | last post by:
Hi, I've written a class for client-socket connection, but I get a lot of times the error message "Unable to read data from the transport connection" when restart reading the stream with socket.BeginRead in the Sub SocketIncomingMsg. I'm debugging now for weeks, but I can't detect where it's going wrong. the part of code where it fails:
5
9358
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 been using the following code for past 4 years, and It was working perfectly fine till Nov-Dec last year. I suspect a security update had messed it up. Please have a look at the following code.
3
10880
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 AutoResetEvent(); if ( !ThreadPool.QueueUserWorkItem(WorkerProc, ev) ) { throw new ApplicationException("fail to queue task"); } ev.WaitOne(5000, false); //timeout == 5000 ms
0
1103
by: Manjunath Premkumar | last post by:
I have an application which uses AutoResetEvent for synchronization. I have deployed this application in a web server. This is working fine. When this application is deployed in environments where loadbalancing is present, this application somehow does not work. So, Does the AutoResetEvent supports the same way in a loadbalanced environment.
4
11107
by: Mr Gray | last post by:
Hi Guys, My requirement is to scan an FTP directory for the presence of 4 files and if a specific file exists I can begin to GET those files, read the contents and save the contents into an SQL 2005 DB. The files are of a fixed length format which map to certain elements of data that my database will exploit. If i can learn to read 1 file and put the contents into the DB then I can do the other 3 on my own. So far I can connect to the...
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.