Running Multiple Threads Simultaneously

Answered Running Multiple Threads Simultaneously

  • 9 martie 2012 12:43
     
      Are cod

    Hi,

    How do I run multiple threads at the same time?

    My code below waits for first one to complete before progressing. How do I run all at same time and wait for all to complete?

    Thanks.

                Func<string, DateTime, bool> method1 = Exceptions.LoadData;
                IAsyncResult item1 = method1.BeginInvoke(this.ID, this.currDate, null, null);
    
                Func<string, DateTime, bool> method2 = RunUsers;
                IAsyncResult item2 = method2.BeginInvoke(this.ID, this.currDate, null, null);
    
                // wait for the call to finish
                item1.AsyncWaitHandle.WaitOne();
                item2.AsyncWaitHandle.WaitOne();
    
                bool result1 = method1.EndInvoke(item1);
                bool result2 = method2.EndInvoke(item2);

Toate mesajele

  • 9 martie 2012 13:24
     
      Are cod

    Hi,

    use the following code:

    ManualResetEvent[] events = { new ManualResetEvent(false), new ManualResetEvent(false) };
                Func<string, DateTime, ManualResetEvent[], bool> methods = Exceptions.LoadData;
                methods += RunUsers;
                IAsyncResult items = methods.BeginInvoke(this.ID, this.currDate, null, null);
    
                //pass wait handles array
                WaitHandle.WaitAll(events);
    
                bool result1 = methods.EndInvoke(items);

    When you finish operations on Exception.LoadData and RunUsers calle ManualResetEvent.Set() in each method.


    Bilhan silva

  • 9 martie 2012 18:20
     
     
    You can take the AsyncWaitHandle's and place them into an array and then wait for all of them with WaitHandle.WaitAll().
  • 9 martie 2012 18:28
     
     
    you can use arrylist & place all of them into arrylist, wait and handle with WaitHandle.WaitAll().
  • 12 martie 2012 10:21
     
     
    Your code waits for the first one to complete, but that doesn't prevent the second one from running at the same time.
    • Marcat ca răspuns de obrienkev 12 martie 2012 13:25
    • Anulare marcare ca răspuns de obrienkev 13 martie 2012 16:35
    •  
  • 12 martie 2012 13:25
     
     
    Your code waits for the first one to complete, but that doesn't prevent the second one from running at the same time.

    thanks. so  item1.AsyncWaitHandle.WaitOne();  will not force one to complete before beginning the other process.

    Is
    item1.AsyncWaitHandle.WaitOne();
    item2
    .AsyncWaitHandle.WaitOne();
    to ensure each process complete?

    Thanks.


    • Editat de obrienkev 12 martie 2012 13:26
    •  
  • 12 martie 2012 16:07
     
     Răspuns

    The two threads run concurrently.

    If your first thread T1 ends before your second thread T2, the first WaitOne will wait until T1 finishes, then the second WaitOne will wait until T2 finishes.

    If T2 ends before T1, the first WaitOne will wait until T1 finishes, then the second WaitOne won't wait at all.

    But you don't really need to call WaitOne. Calling EndInvoke will wait for the thread to complete too. Remove your calls to WaitOne and let the two calls to EndInvoke wait.

  • 13 martie 2012 16:37
     
      Are cod

    Hi,

    The 'complete' flag in my code gets set before one of the processes has been completed. A few seconds later the process completes.

    Why is this?

    Thanks!

                // run processes
                Func<string, DateTime, bool> method1 = LoadTempData.LoadTempUserData;
                IAsyncResult result1 = method1.BeginInvoke(this.ID, this.selectedDate, null, null);
    
                Func<string, DateTime, bool> method2 = LoadTempData.LoadTempNewsData;
                IAsyncResult result2 = method2.BeginInvoke(this.ID, this.selectedDate, null, null);
    
                Func<string, DateTime, bool> method3 = LoadTempData.LoadTempDetailsData;
                IAsyncResult result3 = method3.BeginInvoke(this.ID, this.selectedDate, null, null);
    
                result1.AsyncWaitHandle.WaitOne();
                result2.AsyncWaitHandle.WaitOne();
                result3.AsyncWaitHandle.WaitOne();
    
                bool OKMethod1 = method1.EndInvoke(result1);
                bool OKMethod2 = method2.EndInvoke(result2);
                bool OKMethod3 = method3.EndInvoke(result3);
    
    	   if(OKMethod1 && OKMethod2 && OKMethod3)
    	   {
    		// set complete flag
    		complete = true;
    	   }


    • Editat de obrienkev 13 martie 2012 16:55
    •  
  • 13 martie 2012 19:42
     
     Răspuns

    The code shown here is going to wait for LoadTemp*Data's to complete every time. There isn't any mention of processes in this code, so I don't know which process you may be talking about. You can set break points at the very end of each of the LoadTemp*Data methods, and a breakpoint at the if statement. You'll see that all of the methods complete before the complete flag gets set.

    Is this the only place the complete flag gets set to true?