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

Home Posts Topics Members FAQ

Should AutoResetEvent be Disposed explicitly?

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.Que ueUserWorkItem( WorkerProc, ev) )
{
throw new ApplicationExce ption("fail to queue task");
}
ev.WaitOne(5000 , false); //timeout == 5000 ms
public void WorkProc(object state)
{
AutoResetEvent ev = state as AutoResetEvent;

// do some timecosting job
.....

ev.Set();

}

However, AutoResetEvent inherits from WaitHandle which inherits from
IDisposable. So, I change mo code to
using (AutoResetEvent ev = new AutoResetEvent( ) )
{
if ( !ThreadPool.Que ueUserWorkItem( WorkerProc, ev) )
{
throw new ApplicationExce ption("fail to queue task");
}
ev.WaitOne(5000 , false); //timeout == 5000 ms
};
public void WorkProc(object state)
{
AutoResetEvent ev = state as AutoResetEvent;

// do some timecosting job
.....

ev.Set(); // ObjectDisposedE xception may be thrown here.

}

While debugging, ObjectDisposedE xception may be thrown from WokProc,
because AutoResetEvent might have been disposed in main thread. So,
have to embrace ev.Set with try{} catch {} to get
ObjectDisposedE xception. It is ugly code.

So, what is best solution?

Jun 18 '07 #1
3 10880
On Sun, 17 Jun 2007 20:36:03 -0700, Morgan Cheng
<mo************ @gmail.comwrote :
While debugging, ObjectDisposedE xception may be thrown from WokProc,
because AutoResetEvent might have been disposed in main thread. So,
have to embrace ev.Set with try{} catch {} to get
ObjectDisposedE xception. It is ugly code.

So, what is best solution?
IMHO, it's a bad idea to have a situation in which you attempt to dispose
an object before you know that every other thread is done with it. IMHO,
it's also a bad idea to just give up on a thread, even in the event of a
timeout.

It's fine to have the timeout, but if you're going to do that, you need a
mechanism by which the worker thread and the main thread communicate
status before disposing the event. If you do that, then you won't have a
situation in which the main thread has already disposed the exception
before the worker thread tries to use it.

Pete
Jun 18 '07 #2
On Jun 18, 12:06 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Sun, 17 Jun 2007 20:36:03 -0700, Morgan Cheng

<morgan.chen... @gmail.comwrote :
While debugging, ObjectDisposedE xception may be thrown from WokProc,
because AutoResetEvent might have been disposed in main thread. So,
have to embrace ev.Set with try{} catch {} to get
ObjectDisposedE xception. It is ugly code.
So, what is best solution?

IMHO, it's a bad idea to have a situation in which you attempt to dispose
an object before you know that every other thread is done with it. IMHO,
it's also a bad idea to just give up on a thread, even in the event of a
timeout.
If I tries to add mechanism to sync the disposal of the
AutoResetEvent, I need another AutoResetEvent. Then disposal of that
second AutoResetEvent instance is a problem...

In MSDN sample code, AutoResetEvent is not disposed explicitly.
It's fine to have the timeout, but if you're going to do that, you need a
mechanism by which the worker thread and the main thread communicate
status before disposing the event. If you do that, then you won't have a
situation in which the main thread has already disposed the exception
before the worker thread tries to use it.

Pete

Jun 18 '07 #3
On Jun 18, 12:06 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Sun, 17 Jun 2007 20:36:03 -0700, Morgan Cheng

<morgan.chen... @gmail.comwrote :
While debugging, ObjectDisposedE xception may be thrown from WokProc,
because AutoResetEvent might have been disposed in main thread. So,
have to embrace ev.Set with try{} catch {} to get
ObjectDisposedE xception. It is ugly code.
So, what is best solution?

IMHO, it's a bad idea to have a situation in which you attempt to dispose
an object before you know that every other thread is done with it. IMHO,
it's also a bad idea to just give up on a thread, even in the event of a
timeout.

It's fine to have the timeout, but if you're going to do that, you need a
mechanism by which the worker thread and the main thread communicate
status before disposing the event. If you do that, then you won't have a
situation in which the main thread has already disposed the exception
before the worker thread tries to use it.

Pete

Or, we can make the AutoResetEvent always disposed in worker thread
after AutoResetEvent. Set invocation.

AutoResetEvent ev = new AutoResetEvent( false);
if ( !ThreadPool.Que ueUserWorkItem( WorkerProc, ev) )
{
throw new ApplicationExce ption("fail to queue task");
}
ev.WaitOne(5000 , false); //timeout == 5000 ms
public void WorkProc(object state)
{
AutoResetEvent ev = state as AutoResetEvent;

try
{
// do some timecosting job
.....
}
finally
{
ev.Set();
ev.Close();
}

}

Jun 18 '07 #4

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

Similar topics

7
4448
by: johnnyturk | last post by:
I have a .aspx page on my web site that intermittently fails to load with the following error: Cannot access a disposed object named "System.Net.Sockets.NetworkStream. My hosting company says this is due to the fact that multiple ASP.NET applications are sharing the same server. the only remedy they can offer is for me to pay for a dedicated server (obviously for more hosting $$). Is the hosting company being lazy and not getting to the...
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...
10
1609
by: **Developer** | last post by:
What should I Dispose. For example, in the following what should I Dispose and what happens if I don't? Thanks Dim PS As New PrinterSettings PS.PrinterName = pD.DefaultPageSettings.PrinterSettings.PrinterName
15
1573
by: matko | last post by:
Hi! In one of the two examples for the PaintEventArgs.Graphics-property (in the VS 2005 documentation), the Graphics-object is "saved" to a local variable, before being used. In the other example, no such saving is done. Why was the Graphics-object saved in one of the examples? Is that really necessary? Example #1:
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
0
2483
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a StopReader() method that simply aborts the worker thread (is there a better way for the above situation?). The StopReader creates an ObjectDisposedException when calling t.Abort(). WHY? Public Sub StopReader()
5
9359
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.
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
10648
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...
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,...
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...
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
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.