2010-12-02 97 views
1

下面是我簡單的代碼來啓動5個線程,每一個調用返回發送的值WCF服務,我的問題是:多個線程調用WCF服務

public void clien_GetDataCompleted(object sender, GetDataCompletedEventArgs e) 
     { 
      lock (sync) 
      { 
       count += e.Result; 
      } 
     } 

工作正常,並增加計數器,但我如何捕獲所有線程完成時,有沒有人有簡單的示例代碼如何調用多個使用異步方法的wcf服務。

public partial class Threading : Form 
{ 
     public int count; 
     ServiceReference1.Service1Client clien = new ServiceReference1.Service1Client(); 

     public Threading() 
     { 
      InitializeComponent(); 
     } 

     private void GetData() 
     { 
      clien.GetDataAsync(1); 
     } 

     public void DisplayResults() 
     { 
      MessageBox.Show(count.ToString()); 
     } 

     private object sync = new object(); 

     public void clien_GetDataCompleted(object sender, GetDataCompletedEventArgs e) 
     { 
      lock (sync) 
      { 
       count += e.Result; 
      } 
     } 

     public List<Thread> RunThreads(int count, ThreadStart start) 
     { 
      List<Thread> list = new List<Thread>(); 
      for (int i = 0; i <= count - 1; i++) 
      { 
       dynamic thread = new Thread(start); 
       thread.Start(); 
       list.Add(thread); 
      } 
      return list; 
     } 

     private void button1_Click_1(object sender, EventArgs e) 
     { 
      clien.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(clien_GetDataCompleted); 
      ThreadStart WcfCall = new ThreadStart(GetData); 
      IList<Thread> threads = RunThreads(5, WcfCall); 
     } 

    } 

千恩萬謝

回答

0

如果您使用的是.NET 4.0中,您可以使用任務並行庫(TPL)和使用任務,而不是主題。任務有更多的流量控制。你可以用任務做的東西像

// Wait for all the tasks to finish. 
    Task.WaitAll(tasks); 

這裏例如如何使用任務,等待所有任務完成。 here

+0

感謝弗拉德,我就給你一展身手。 – 2010-12-02 14:37:34

0

我已經實現了使用任務的解決方案,代碼如下,其作品很好,讓我知道如果我能做的任何改進。

public partial class Tasks : Form 
{ 
    static ServiceReference1.Service1Client clien = new ServiceReference1.Service1Client(); 
    int count = 0; 

    public Tasks() 
    { 
     InitializeComponent(); 
    } 

    // Define a delegate that prints and returns the system tick count 
    Func<object, int> action = (object obj) => 
    { 
     int i = (int)obj; 
     clien.GetDataAsync(i); 

     Console.WriteLine("Task={0}, i={1}, Thread={2}", Task.CurrentId, i, Thread.CurrentThread.ManagedThreadId); 

     return i; 
    }; 

    public void clien_GetDataCompleted(object sender, GetDataCompletedEventArgs e) 
    { 
     count += e.Result; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     const int n = 5; 

     // create async callback delegate from wcf. 
     clien.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(clien_GetDataCompleted); 

     // Construct started tasks 
     Task<int>[] tasks = new Task<int>[n]; 
     for (int i = 0; i < n; i++) 
     { 
      tasks[i] = Task<int>.Factory.StartNew(action, i); 
     } 

     try 
     { 
      // Wait for all the tasks to finish. 
      Task.WaitAll(tasks); 

      MessageBox.Show(count.ToString()); 
     } 
     catch 
     { 

     } 

    } 
} 

歡呼

+0

看起來不錯。我唯一要做的就是檢查client_GetDataCompleted是否有錯誤。 if(e.Cancelled) return;如果(e.Error!= null) else e .Error; else count + = e.Result; – 2010-12-02 15:24:54