+ Reply to Thread
Results 1 to 5 of 5
  1. Member
    Join Date
    Sep 2009
    Posts
    93
    #1

    Default How to use WaitCallback to synchronize threadOne and threadTwo so that it prevent exe

    How to use WaitCallback to synchronize threadOne and threadTwo so that it prevent execution of threadOne until threadTwo completes execution?

    I though waitcallback just push the function and it will run whenever there is a thread in the pool, isn't it?

    but I saw a question said that waitcallback would prevent threadOne until threadTwo complete exec.
    Thank you
    Reply With Quote Quote  

  2. SS -->
  3. Junior Member
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    29

    Certifications
    MCP sql server, MCTS Web, MCTS Windows, MCTS Distributed - MCPD Enterprise Developer
    #2
    Quote Originally Posted by poguy View Post
    How to use WaitCallback to synchronize threadOne and threadTwo so that it prevent execution of threadOne until threadTwo completes execution?

    I though waitcallback just push the function and it will run whenever there is a thread in the pool, isn't it?

    but I saw a question said that waitcallback would prevent threadOne until threadTwo complete exec.
    Thank you
    If thread-one has to wait for thread two to finish, then you might as well just execute the operations synchronously and eliminate the overhead of creating threads
    Reply With Quote Quote  

  4. Junior Member
    Join Date
    Dec 2009
    Posts
    5
    #3
    The ThreadPool.QueueUserWorkItem override that takes one parameter, a WaitCallback delegate, definitely will not block that thread.

    I wonder if the question that you saw, that seemed to suggest that the thread WOULD wait, was doing something a bit more complicated.

    70-536 does discuss passing an EventWaitHandle object to the callback delegate. After queuing threads, the main thread calls WaitHandle.WaitAll (which is passed an array of those EventWaitHandle objects). At that point it will block.

    When completing, the background thread calls that object's Set method. The main thread therefore resumes only after all those objects have had their state reset by the background threads.

    But the simplistic ThreadPool.QueueUserWorkItem simply passing a delegate will definitely not block.
    Reply With Quote Quote  

  5. Junior Member
    Join Date
    Dec 2009
    Posts
    5
    #4
    Just to clarify, given...

    publicstaticvoid TestThreadPoolA()
    {
    // Queue the task.
    ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc));
    ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc));
    Console.WriteLine("Main thread resumes.");
    }
    publicstaticvoid TestThreadPoolB()
    {
    // Queue the task.
    AutoResetEvent e1 = newAutoResetEvent(false);
    ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc),e1);
    AutoResetEvent e2 = newAutoResetEvent(false);
    ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc), e2);
    WaitHandle.WaitAll(newWaitHandle[] { e1, e2});
    Console.WriteLine("Main thread resumes.");
    }
    // This thread procedure performs the task.
    staticvoid ThreadProc(Object stateInfo)
    {
    Console.WriteLine("Starting a thread.");
    Thread.Sleep(3000);
    AutoResetEvent e = (AutoResetEvent)stateInfo;
    Console.WriteLine("Finishing a thread");
    if (e != null)
    {
    e.Set();
    }
    }

    If I ran TestThreadPoolA, I woudl see something like...

    Main thread resumes
    Starting a thread
    Starting a thread
    Finishing a thread
    Finishing a thread

    If I ran TestThreadPoolB, I woudl see something like...

    Starting a thread
    Starting a thread
    Finishing a thread
    Finishing a thread
    Main thread resumes
    Last edited by davidinnz; 12-18-2009 at 09:18 PM.
    Reply With Quote Quote  

  6. Junior Member Registered Member
    Join Date
    May 2011
    Posts
    1
    #5
    If you ask this question because it's an answer of the 70-536 exam, i think the answer could be by setting thread pool SetMaxThreads to1. So queue firstly thred2 then thread1. Plusthe pool reuse of the thread should fasten the process.
    Reply With Quote Quote  

+ Reply to Thread

Social Networking & Bookmarks