Quesiton about WaitOne(Int32, Boolean)

  • Thread starter Thread starter linuxfedora
  • Start date Start date
Hi All,

I have read the document in Microsoft about the exitContext in
WaitOne, but i still don't understand when to use autoEvent.WaitOne
(1000, true) or autoEvent.WaitOne(1000, false)

http://msdn.microsoft.com/en-us/library/kzy257t0.aspx

Can anyone give me more information about their different?Thanks

Use the manual reset event: ManualResetEvent. It forces you to Set
and Reset, and IMO is better than using the "automatic" you refer to.

The design context is as follows:
public static ManualResetEvent mrevent = new ManualResetEvent
(false); //used to make thread2 wait for thread1, which it depends on
below

Thread ThreadFirst, ThreadLast; //in constructor

private void AnyfunctionHere ()
{
ThreadFirst = new Thread(new ThreadStart(Function1));

if (ThreadFirst.IsAlive == false)
{
ThreadFirst.Start();

}

ThreadLast = new Thread(new ThreadStart(Function2));
if (ThreadLast.IsAlive == false)
{

ThreadLast.Start();
}


}

private void Function1()
{
//do stuff here
mrevent.Set(); //at end, unblocks other threads
}


private void Function2()
{

bool myBoolStartThisThreadYet = false;
myBoolStartThisThreadYet = mrevent.WaitOne(); //blocks this thread
until Function1 is done

if (myBoolStartThisThreadYet != true) return;
int myInt = 123;
//do stuff here that has to happen after Function1
//if you get runtime exception for certain portion of code, put that
code below inline as shown here (you can also do the same thing in
Function 1). Keep in mind there seems to be a slight performance
penalty for doing this, but it's sometimes the only way you can make
it work:

//note format!

this.Dispatcher.BeginInvoke(delegate()
{
//put runtime exception code here. You can use variables from
Function2 here (in scope)
int j = myInt; //OK
}
);

mrevent.Reset(); //sets thread2 to block mode (opposite of set).
}


RL
 
linuxfedora said:
Hi All,

I have read the document in Microsoft about the exitContext in
WaitOne, but i still don't understand when to use autoEvent.WaitOne
(1000, true) or autoEvent.WaitOne(1000, false)

http://msdn.microsoft.com/en-us/library/kzy257t0.aspx

Can anyone give me more information about their different?Thanks

The short answer is: if you have to ask, you don't need the flag.

The doc page for the method overload (your link) has a discussion,
"Notes on Exiting the Context", that elaborates somewhat. Basically, a
synchronization context imposes additional synchronization rules beyond
the normal synchronization objects, and while calling WaitOne() it may
be useful to release the synchronization based on those rules if
currently being held.

This is somewhat analogous to the Monitor.Wait() method, which releases
its lock while waiting, but instead allows for the use of the WaitHandle
sub-classes for the signaling part of the synchronization, while using
the context for locking.

At least, that's my understanding. It's more of a server/COM thing,
which I don't personally have to deal with much, so I all my knowledge
on the topic is academic.

You may find these pages helpful in elaboration on the synchronization
context aspect, if you're still curious:
http://msdn.microsoft.com/en-us/library/system.contextboundobject.aspx
http://msdn.microsoft.com/en-us/library/system.runtime.remoting.contexts.contextattribute.aspx

Pete
 
Back
Top