Click here to Skip to main content
16,008,469 members
Home / Discussions / C#
   

C#

 
AnswerRe: VS2005 Cross Thread Pin
Judah Gabriel Himango22-May-06 9:25
sponsorJudah Gabriel Himango22-May-06 9:25 
GeneralRe: VS2005 Cross Thread Pin
donkaiser23-May-06 6:59
donkaiser23-May-06 6:59 
GeneralRe: VS2005 Cross Thread Pin
Judah Gabriel Himango23-May-06 15:37
sponsorJudah Gabriel Himango23-May-06 15:37 
GeneralRe: VS2005 Cross Thread Pin
donkaiser23-May-06 7:13
donkaiser23-May-06 7:13 
QuestionCustom SOAP Header Pin
robertschoenstein22-May-06 8:01
professionalrobertschoenstein22-May-06 8:01 
QuestionVertical scrollbar broken after upgrade to VS2005 [modified] Pin
rick.wirch22-May-06 6:28
rick.wirch22-May-06 6:28 
QuestionHow to avoid dragging a form Pin
Elvia Gonzalez22-May-06 6:24
Elvia Gonzalez22-May-06 6:24 
QuestionManualResetEvent troubles Pin
Judah Gabriel Himango22-May-06 5:59
sponsorJudah Gabriel Himango22-May-06 5:59 
Hi guys

I'm having some difficulty with the System.Threading.ManualResetEvent.

My function is listed below. Excuse the relatively large function; scroll down towards the end and you'll see the line I'm having trouble with.

private static bool WaitForCompletionOrTimerExpiration(Function method, int timeout)
     {
         using (ManualResetEvent threadWaitHandle = new ManualResetEvent(false))
         {
             bool hasFinishedExecutingMethod = false;
             int hasSignalled = 0;

             // Create a timer callback. When the timer callback hits, the wait handle will be set.
             TimerCallback timerCallback = delegate(object state)
             {
                 int originalValueOfHasSignalled = Interlocked.CompareExchange(ref hasSignalled, 1, 0);
                 if (originalValueOfHasSignalled == 0)
                 {
                     threadWaitHandle.Set();
                 }
             };

             // Create a method to be called on the threadpool that will invoke the method passed to us and set the handle when done executing.
             Exception methodException = null;
             WaitCallback threadPoolMethod = delegate(object state)
             {
                 try
                 {
                     method();
                 }
                 finally
                 {
                     int originalValueOfHasSignalled = Interlocked.CompareExchange(ref hasSignalled, 1, 0);
                     if (originalValueOfHasSignalled == 0)
                     {
                         hasFinishedExecutingMethod = true;
                         threadWaitHandle.Set();
                     }
                 }
             };

             // Create and run our timer, start running the method on the thread pool, wait on the wait handler until someone signals us.
             // We will be signaled when either the timer expires or when the method finishes executing.
             using (Timer timer = new Timer(timerCallback, null, timeout, Timeout.Infinite))
             {
                 ThreadPool.QueueUserWorkItem(threadPoolMethod);

                 //
                 // THIS IS THE LINE I'M HAVING TROUBLE WITH
                 //
                 threadWaitHandle.WaitOne(); // BUG! Sometimes this will wait forever!
             }

             return hasFinishedExecutingMethod;
         }
     }


Notice the WaitOne() call; sometimes this call blocks forever! In what circumstance will thread WaitOne() call block forever?

Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Islamic Domination: Coming to a Jewish state near you!
The apostle Paul, modernly speaking: Epistles of Paul

Judah Himango


AnswerRe: ManualResetEvent troubles Pin
leppie22-May-06 6:33
leppie22-May-06 6:33 
GeneralRe: ManualResetEvent troubles Pin
Judah Gabriel Himango22-May-06 7:15
sponsorJudah Gabriel Himango22-May-06 7:15 
GeneralRe: ManualResetEvent troubles Pin
leppie22-May-06 7:34
leppie22-May-06 7:34 
GeneralRe: ManualResetEvent troubles Pin
Judah Gabriel Himango22-May-06 7:38
sponsorJudah Gabriel Himango22-May-06 7:38 
GeneralRe: ManualResetEvent troubles Pin
Judah Gabriel Himango22-May-06 9:21
sponsorJudah Gabriel Himango22-May-06 9:21 
QuestionThreads, Events & BeginInvoke Pin
iswoolley22-May-06 5:11
iswoolley22-May-06 5:11 
AnswerRe: Threads, Events & BeginInvoke [modified] Pin
Dustin Metzgar22-May-06 6:06
Dustin Metzgar22-May-06 6:06 
GeneralRe: Threads, Events & BeginInvoke [modified] Pin
Iain W22-May-06 9:49
Iain W22-May-06 9:49 
AnswerRe: Threads, Events & BeginInvoke Pin
Judah Gabriel Himango22-May-06 6:13
sponsorJudah Gabriel Himango22-May-06 6:13 
GeneralRe: Threads, Events & BeginInvoke Pin
Iain W22-May-06 9:58
Iain W22-May-06 9:58 
GeneralRe: Threads, Events & BeginInvoke Pin
Judah Gabriel Himango22-May-06 10:16
sponsorJudah Gabriel Himango22-May-06 10:16 
QuestionNeed help Pin
engsrini22-May-06 4:56
engsrini22-May-06 4:56 
AnswerRe: Need help Pin
Judah Gabriel Himango22-May-06 6:16
sponsorJudah Gabriel Himango22-May-06 6:16 
GeneralRe: Need help Pin
engsrini22-May-06 6:37
engsrini22-May-06 6:37 
QuestionType or namespace name could not be found Pin
msolh22-May-06 4:17
msolh22-May-06 4:17 
AnswerRe: Type or namespace name could not be found Pin
User 665822-May-06 5:11
User 665822-May-06 5:11 
GeneralRe: Type or namespace name could not be found Pin
msolh22-May-06 7:57
msolh22-May-06 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.