473,811 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A problem with AutoResetEvent

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.
[Please note that we are using .NET Framework 1.1 SP1 for development]

-----------------------------------
Thread 1:
--------
public void IRCSendQueue(Xm lDocument doc, string ircdest,bool
bExpectAck)
{
lock(queueIRCSe nd)
{
queueIRCSend.En queue(new IRCSendStruct(d oc,ircdest,bExp ectAck));
}
ircSendSignal.S et();
}

Thread 2:
--------
protected void IRCSendingThrea d()
{
//Event Processing
IRCSendStruct ircSendStruct;
try
{
while(ircclient .Connected)
{
ircSendSignal.W aitOne();
while(queueIRCS end.Count 0)
{
lock(queueIRCSe nd)
{
ircSendStruct = (IRCSendStruct) queueIRCSend.De queue();
}
//Process IRC Message
this.IRCSend((i rcSendStruct.ex pectAck)?ircSen dStruct.doc.Doc umentElement
..SelectSingleN ode(Nodes.SEQNU M).InnerText:nu ll,
Utility.DecodeX ml(ircSendStruc t.doc),ircSendS truct.ircdest);
}
}
}
catch(ThreadAbo rtException)
{
Log("IRC Sending Thread: Thread Abort", LogEntryType.IN FORMATION,
null);
}
}
-----------------------------------
I put in logging before and after Set and WaitOne, It Sets the
AutoResetEvent but WaitOne never returns.

Thanks in advance.
Regards,

Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Mar 28 '07 #1
5 9359
Maqsood Ahmed <ma***********@ gmail.comwrote:
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.
[Please note that we are using .NET Framework 1.1 SP1 for development]
It's hard to separate out the threading code from the rest in the
snippets you posted.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 28 '07 #2
Maqsood,

I don't think you should be using an event in this case. You are using
a producer/consumer pattern, and you are better off using the lock
statement, along with the static Pulse and Wait methods on the Monitor
object. This is how I would write your code:

Thread 1:
--------
public void IRCSendQueue(Xm lDocument doc, string ircdest,bool bExpectAck)
{
lock(queueIRCSe nd)
{
queueIRCSend.En queue(new IRCSendStruct(d oc,ircdest,bExp ectAck));

// Pulse the thread which will process the item placed in the
queue.
Monitor.Pulse(q ueueIRCSend);
}
}

Thread 2:
--------
protected void IRCSendingThrea d()
{
//Event Processing
IRCSendStruct ircSendStruct;

// Lock on the object here.
lock (queueIRCSend)
{
// Do while connected.
while (ircclient.Conn ected)
{
// Wait on the object.
Monitor.Wait(qu eueIRCSend);

// Perform processing here.
}
}
catch(ThreadAbo rtException)
{
Log("IRC Sending Thread: Thread Abort", LogEntryType.IN FORMATION,
null);
}
}

Using this method, you should only have one thread that is listening
(the thread calling Monitor.Wait), while you can have multiple threads
sending process requests (the threads calling Pulse). When you call Pulse,
you are priming the thread that has called Wait to be called when you exit
the lock block.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Maqsood Ahmed" <ma***********@ gmail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
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.
[Please note that we are using .NET Framework 1.1 SP1 for development]

-----------------------------------
Thread 1:
--------
public void IRCSendQueue(Xm lDocument doc, string ircdest,bool
bExpectAck)
{
lock(queueIRCSe nd)
{
queueIRCSend.En queue(new IRCSendStruct(d oc,ircdest,bExp ectAck));
}
ircSendSignal.S et();
}

Thread 2:
--------
protected void IRCSendingThrea d()
{
//Event Processing
IRCSendStruct ircSendStruct;
try
{
while(ircclient .Connected)
{
ircSendSignal.W aitOne();
while(queueIRCS end.Count 0)
{
lock(queueIRCSe nd)
{
ircSendStruct = (IRCSendStruct) queueIRCSend.De queue();
}
//Process IRC Message
this.IRCSend((i rcSendStruct.ex pectAck)?ircSen dStruct.doc.Doc umentElement
SelectSingleNod e(Nodes.SEQNUM) .InnerText:null ,
Utility.DecodeX ml(ircSendStruc t.doc),ircSendS truct.ircdest);
}
}
}
catch(ThreadAbo rtException)
{
Log("IRC Sending Thread: Thread Abort", LogEntryType.IN FORMATION,
null);
}
}
-----------------------------------
I put in logging before and after Set and WaitOne, It Sets the
AutoResetEvent but WaitOne never returns.

Thanks in advance.
Regards,

Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***

Mar 28 '07 #3
Hello Mr. Nicholas Paldino,

Thanks for your reply. I will use this code snippet to see its affect
on the overall performance.

Regards,

Maqsood Ahmed

On Mar 28, 10:21 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
Maqsood,

I don't think you should be using an event in this case. You are using
a producer/consumer pattern, and you are better off using the lock
statement, along with the static Pulse and Wait methods on the Monitor
object. This is how I would write your code:
Apr 2 '07 #4
Hello Mr. Paldino,

I tried the code you provided. Just some thoughts on it:

1. Don't you think that the 'lock' statement before Wait method will
produce a deadlock, because the Pulse method is also in the lock
statement.
2. Secondly, we are currently working on signal based communication
between threads. I don't think Monitor will allow us to do this. Here
are the two descriptions according to MSDN:

Monitor: "The Monitor class controls access to objects by granting a
lock for an object to a single thread".
AutoResetEvent: "AutoResetE vent allows threads to communicate with
each other by signaling".

We can use lock statements for granting a lock to a single thread
(similar to Monitor.Enter/Monitor.Exit).

3. Also, Will the Wait method proceed, if Pulse is called before Wait?
AutoResetEvent. Wait proceeds after .Set is called first.

Please clarify these doubts.

Thanks in advance,

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net

Apr 9 '07 #5
Maqsood Ahmed <ma***********@ gmail.comwrote:
I tried the code you provided.
And did it deadlock? Did it show any of the problems you were concerned
about?
Just some thoughts on it:

1. Don't you think that the 'lock' statement before Wait method will
produce a deadlock, because the Pulse method is also in the lock
statement.
No. Monitor.Wait releases the lock.
2. Secondly, we are currently working on signal based communication
between threads. I don't think Monitor will allow us to do this.
You're wrong - the Wait and Pulse methods are precisely there for
signalling.
3. Also, Will the Wait method proceed, if Pulse is called before Wait?
AutoResetEvent. Wait proceeds after .Set is called first.
No - Wait requires that Pulse is called *after* the call to Wait before
it will signal the waiting thread. Sometimes that's helpful, sometimes
it's not. A typical use of Wait has a while loop (within the lock) so
it knows whether or not to call Wait in the first place (and possibly
to call it multiple times).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 9 '07 #6

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

Similar topics

10
3597
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsicollectionclasssyncroottopic.asp) And I'm interested in locking an ArrayList during the entire enumeration, as shown in the example code. My problem is that I'm STILL...
2
12559
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
3199
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
6
3134
by: Ken | last post by:
When running a program in the debugger, what would cause it to crash without any error messages? I get "The program has exited with code 0 (0x0)". The program is a MDI app with threading for several serial ports. It only crashes when data is being received on one or more of the serial ports. Could someone please give me some ideas about what would cause a program to terminate in this way? Thanks....
1
3047
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 serial port is 23 bytes long, and is sent in response to a command. I begin timing before sending the command, and display split times when received data is signalled. The entire exchange of command and response is over within 6 ms (I have checked...
0
3204
by: Wayne Sepega | last post by:
I have a C# windows service that uses MSMQ, DTC Transactions, SQL server 2005 and Notification services. I am doing the following in the service: OnStart Create the queue set the Peek Complete event call Begin Peek
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
5934
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 AutoResetEvent, but I don't think it's the best practice....
0
10389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10402
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10135
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7670
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
6890
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
5554
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
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3018
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.