I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 219, comments - 2287, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

Converting System.Threading.WaitHandle.Handle to System.Threading.WaitHandle.SafeWaitHandle

I had to convert a project from .Net 1.1 to .Net 2.0 this week.
It built ok with a few warnings. Amongst others there was this one:
Warning: 'System.Threading.WaitHandle.Handle' is obsolete: 'Use the SafeWaitHandle property instead.'

That's cool, no problem, right? As it turns out it's not just replacing one property with another.
Handle property is IntPtr type and SafeWaitHandle is a class.
So .Net 2.0 has a new class called SafeWaitHandle which represents a wrapper class for a wait handle.
Why it's there is well explained on BCLTeam's blogs here.

So what to do?

It preety simple in the end.
instead of
_Event.Handle;
just use
_Event.SafeWaitHandle.DangerousGetHandle();

where _Event is in my case AutoResetEvent type.

Print | posted on Friday, June 23, 2006 4:50 PM | Filed Under [ .Net ]

Feedback

Gravatar

# re: Converting System.Threading.WaitHandle.Handle to System.Threading.WaitHandle.SafeWaitHandle

I would like to thank you for your post. I was trying to update a DirectSound example and ran into this problem. Your solution seemed to do the trick.

Thomas Patten
6/27/2007 5:12 AM | Thomas Patten
Gravatar

# re: Converting System.Threading.WaitHandle.Handle to System.Threading.WaitHandle.SafeWaitHandle

you're welcome.
6/27/2007 10:13 AM | Mladen
Gravatar

# re: Converting System.Threading.WaitHandle.Handle to System.Threading.WaitHandle.SafeWaitHandle

Hello,

May I ask if using the .SafeWaitHandle.DangerousGetHandle would release the handle of the AutoResetEvent?

I have a problem of doing this piece of code:
***********************
For my main Program.cs:
***********************
for (int i = 0; i < JobNumber; i++)
{
doneEvents[i] = new AutoResetEvent(false);
if (i + 1 == JobNumber) jobqueue = true;
JobProcess f = new JobProcess(r.Next(20, 40), doneEvents[i], jobqueue);
arrJob[i] = f;
ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);

}

WaitHandle.WaitAll(doneEvents);
Console.WriteLine("Process stops here");
...

***********************
For my calling class JobProcess.cs:
***********************

public void ThreadPoolCallback(Object threadContext)
{ int threadIndex = (int)threadContext;
Console.WriteLine("job thread {0} started... at n:{1}", threadIndex, _n);
_JobN = Calculate(_n);
Console.WriteLine("job thread {0} result calculated...", threadIndex);
/*_doneEvent.Set(); */
if (_endOfJobQueue == true)
{ _doneEvent.SafeWaitHandle.SetHandleAsInvalid();
_doneEvent.SafeWaitHandle.DangerousGetHandle();
//SafeWaitHandle.DangerousRelease();
Console.WriteLine(" is closed?: " + _doneEvent.SafeWaitHandle.IsClosed.ToString() +
"\r\n timeout:" + WaitHandle.WaitTimeout.ToString());
}
else{
_doneEvent.WaitOne();
}
}


What I am trying to do is to set QueueUserWorkItem to run the ThreadPoolCallback in such a way I will do synchronization. But the problem when I run my code. It never reached the Console.WriteLine("Process stops here")


where as if I comment the
if (_endOfJobQueue == true)
{ ... }
else{
_doneEvent.WaitOne();
}

and replaced is by just _doneEvent.Set(); It works and it reaches the Console.WriteLine("Process stops here") but the synchronization of the _doneEvent is not observed.

What I want to attain is how to release, or perhaps signal the WaitHandle.WaitAll() that there is nothing more to wait but proceed to exit and close the thread queue.

Thank You.
9/5/2007 5:50 AM | Jan Palmer
Gravatar

# re: Converting System.Threading.WaitHandle.Handle to System.Threading.WaitHandle.SafeWaitHandle

no idea on that one Jan.
look with reflector what it does under the hood.
9/5/2007 2:50 PM | Mladen
Gravatar

# re: Converting System.Threading.WaitHandle.Handle to System.Threading.WaitHandle.SafeWaitHandle

You can use the SafeWaitHandle.DangerousGetHandle method. So your code will look like this:


wo.hEvent = writeEvent.SafeWaitHandle.DangerousGetHandle();



If you want to know why, please read this blog from Brian Grunkemeyer: SafeHandle: A Reliability Case Study.

http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx
3/3/2008 3:21 AM | chan
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET