2014-06-05 61 views
0

我有以下代码,我想转换为使用C#4.0的多线程。有可能这样做吗?任何指导非常感谢。将单线程C#代码转换为多线程

我有一个按钮开始启动过程,它调用下面的函数

private void ProcessData() 
{ 
    //clear some ui text fields and disable start button and enable cancel button and set status to working 
    //open database connection 
    try 
    { 
     //populate ui multi line textbox saying that it is getting data from database 
     var dsResult = new DataSet(); 
     //populate dataset 
     //populate ui multi line textbox saying that it finished getting data from database 
     //close connection 

     if (dsResult.Tables.Count == 1 && dsResult.Tables[0].Rows.Count > 0) 
     { 
      //populate another field saying how much records we got 
      int iCount = 1; 
      foreach (DataRow dr in dsResult.Tables[0].Rows) 
      { 
       if (_stop) 
       { 
        //set the status as forced stop 
        return; 
       } 
       //populate the currently processed record count using iCount 
       //populate ui multi line textbox indicating which item that it is starting to work using dr["Item"] 
       //call some external function to process some data, inside this function i have to update ui multi line textbox as well 
       var dataFile = SearchDataFile(dr["Item"].ToString()); 
       if (dataFile == null) 
       { 
        //populate ui multi line textbox indicating that item was not found 
        iCount++; 
        continue; 
       } 
       //call another external function to process some data, inside this function i have to update ui multi line textbox as well 
       UpdateDataFile(dataFile, folderId, dr, dr["Item"].ToString()); 
       iCount++; 
      } 
     } 
     else 
     { 
      //populate ui multi line textbox indicating no data found 
     } 
     //update status saying that it is complete 
     tsslblStatus.Text = "STATUS : COMPLETE"; 
    } 
    catch (Exception ex) 
    { 
     //close connection 
     //populate ui multi line textbox indicating error occured 
     //update status to error 
    } 
    finally 
    { 
     //re adjust ui and enabling start and disable stop 
     //set _stop variable to false 
    } 
} 

感谢

+0

你,如果你的应用程序遇到任何进展缓慢?你希望我们移动哪个部分? – Complexity

+0

请注意多线程和GUI应用程序。多线程可能会使您的GUI无响应。 – BossRoss

+1

@BossRoss,为什么呢? – Sinatr

回答

0

先拆你的逻辑

SomeJobA(); 
SomeJobB(); 
SomeJobC(); 
... 

然后做一些多线程

start SomeJobA() thread/task 
start SomeJobB() thread/task 
start SomeJobC() thread/task 
... 
to wait or not to wait for them to finish? 

要从其他线程更新UI,请使用Invoke/BeginInvoke

+0

谢谢,我会尝试这 –

0

我发现的最简单的方法是使用Parallel.ForEach方法,所以不是

foreach (DataRow dr in dsResult.Tables[0].Rows) 

使用

Parellel.Foreach(dsResult.Tables[0].Rows, dr => 
{ 
    //foreach body code goes here. 
}); 

然而,如果你尝试更新的算法,操纵用户界面要利用并发性,你将会有一段糟糕的时间。 Win窗体应用程序(如果我正确记得,Win 8 /电话应用程序)做而不是允许您从主线程以外的任何线程操纵UI(即写入文本框)。

为了让您正确地并行化该算法,您需要分离出操纵UI的所有代码。

可以使用TaskFactory编组出你想要并行做的工作:

public class MyState{ 
    public string Example {get;set;} 
} 

private MyState _state; 

private void MethodCalledFromUIThread() 
{ 
    //Update UI. 
    TextBox1.Text = string.Empty; 

    //Start parallel work in a new thread. 
    new TaskFactory().StartNew(() => ThreadedMethod()) 
     //Wait for background threads to complete 
     .Wait(); 

    //Update UI with result of processing. 
    TextBox1.Text = _state.Example; 
} 

private void ThreadedMethod() 
{ 
    //load dsResult 

    Parallel.ForEach(dsResult.Tables[0].Rows, dr => 
    { 
     //process data in parallel. 
    } 

    //Update the State object so the UI thread can get access to the data 

    _state = new MyState{Example = "Data Updated!";} 
} 
+0

谢谢我会尝试这种方法以及 –