2011-10-20 37 views
1

我打了幾個按鈕點擊的方法。線程的按鈕點擊方法

泛函()

functionB()

functionC()

所有這三種功能是相互獨立的並且它們需要長的時間來執行。我檢查並發現,通過線程,我可以將所有三者一起運行,這將節省執行時間。

由於我是線程概念的新手,任何人都可以請指導我以最簡單的方式在場景或其他方式中進行線程處理,這在此場景中將非常有用。在同一個函數

編輯

還有一個問題:

我綁定了三個功能執行後5個GridView的。像這樣

  gv1.DataSource = GetData("Mill"); 
      gv1.DataBind(); 

      gv2.DataSource = GetData("Factory"); 
      gv2.DataBind(); 

      gv3.DataSource = GetData("Garage"); 
      gv3.DataBind(); 

      gv4.DataSource = GetData("Master"); 
      gv4.DataBind(); 

他們都使用相同的方法獲得結果,他們也花費時間加載。有什麼辦法可以讓它們平行運行嗎?我擔心,因爲他們使用相同的方法來獲取數據。是否有可能爲他們進行線程化?怎麼樣 ?

回答

1

我不知道Parallel.Invoke()如何決定什麼並行執行,但如果你希望保證他們將並行執行,使用線程:

var t1 = new Thread(MySlowFunction); 
t1.IsBackground = true; 
t1.Start(); 

var t2 = new Thread(MySlowFunction); 
t2.IsBackground = true; 
t2.Start(); 

# To resync after completion: 
t1.Join(); 
t2.Join(); 

甚至更​​好,使用線程池:

ThreadPool.QueueUserWorkItem(MyWork); 

記得來處理你的線程例外。

+0

感謝您的回答,請幫我編輯問題。 – Zerotoinfinity

+0

至於編輯問題,完全取決於GetData是否是線程安全的。我不知道該功能如何獲取數據。 – Fantius

0

嘗試使用System.Threading.Tasks命名空間

喜歡的東西

var task1 = Task.Factory.StartNew(() => DoA()); 
var task2 = Task.Factory.StartNew(() => DoB()); 
var task3 = Task.Factory.StartNew(() => DoC()); 
Task.WaitAll(task1, task2, task3); 

http://www.codethinked.com/net-40-and-systemthreadingtasks

+0

謝謝您的回答,請幫我編輯的問題了。 – Zerotoinfinity

0

這裏將並行執行4個任務的例子:

public partial class _Default : System.Web.UI.Page 
{ 
    public class MyViewModel 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void BtnBindClick(object sender, EventArgs e) 
    { 
     // we define the input for the tasks: each element consists 
     // of the grid we are willing to bind the results at the end and 
     // some optional parameter we want to pass to the GetData function 
     var inputs = new[] 
     { 
      new { Grid = gv1, Input = "Mill" }, 
      new { Grid = gv2, Input = "Factory" }, 
      new { Grid = gv3, Input = "Garage" }, 
      new { Grid = gv4, Input = "Master" }, 
     }; 

     // define the tasks we want to execute in parallel 
     var tasks = inputs 
      .Select(x => Task.Factory.StartNew(
       () => new { Grid = x.Grid, Output = GetData(x.Input) }) 
      ) 
      .ToArray(); 

     // define a task which will be executed once all tasks have finished 
     var finalTask = Task.Factory.ContinueWhenAll(tasks, x => x); 

     // wait for the final task 
     finalTask.Wait(); 

     // consume the results 
     foreach (var item in finalTask.Result) 
     { 
      if (item.Exception == null) 
      { 
       // if no exception was thrown for this task we could bind the results 
       item.Result.Grid.DataSource = item.Result.Output; 
       item.Result.Grid.DataBind(); 
      } 
     } 
    } 

    private MyViewModel[] GetData(string input) 
    { 
     // Simulate slowness 
     Thread.Sleep(1000); 
     return Enumerable.Range(1, 5).Select(x => new MyViewModel 
     { 
      Id = x, 
      Name = input 
     }).ToArray(); 
    } 
}