2015-04-20 51 views
-1

實際上我學習(艱難地),C#和被一個問題爭取天:C# - 如何從一個線程在另一個類更新主UI

我用WPF寫我的第一個C#應用程序(dotNet 4.0)。當我點擊一個按鈕時,會使用BackgroundWorker線程並從外部類調用一個方法,這樣我的UI就不會凍結 - >我的方法按預期運行。

然後我試圖從thos外部類更新一個ListView控件來獲得某種進度(文本),我悲慘地失敗了。

我知道我需要使用委託和調度程序來更新我的控件。

我試着用這裏提供的解決方案How to update UI from another thread running in another class。 (我不能評論它,因爲我的代表低),我想念一些難題。

什麼YourEventArgs(狀態)是指?當我的方法在BGW中運行時,我只是無法啓動事件並將內容傳回給我的用戶界面。

到目前爲止,我有這段代碼(從答案更新):

namespace AppMain 
{ 
    public partial class MainWindow 
    { 
     BackgroundWorker AppWorker = new BackgroundWorker(); 

     public MainWindow() 
     { 
      InitializeComponent(); 

      AppWorker.WorkerSupportsCancellation = true; 
      AppWorker.DoWork += AppWorker_DoWork; 
      AppWorker.RunWorkerCompleted += AppWorker_RunWorkerCompleted; 
      } 

     private void btnLoad_Click(object sender, RoutedEventArgs e) 
     { 
      lstTest.Items.Add("Processing data..."); 
      AppWorker.RunWorkerAsync(); 
     } 

     public void AppWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) 
     { 
      SetXmlData xml = new SetXmlData(); 
      xml.ProgressUpdate += (s, evt) => 
      { 
       Dispatcher.BeginInvoke((Action)(() => 
       { 
        lstTest.Items.Add("this is a test : " + evt.myData); //how to retrieve the myData property from evt ? 
       })); 
      }; 
      xml.FlushData(); 
     } 

     public void AppWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) 
     { 
      if (!(e.Cancelled)) 
      { 
       lstTest.Items.Add("Done"); 
      } 
      else 
      { 
       MessageBox.Show("Cancelled"); 
      } 
     } 
} 
} 

SetXmlData.cs

namespace AppMain 
{ 
    public class SetXmlData 
    { 
    public event EventHandler ProgressUpdate; 

     //update method 
     public void update(object input) 
     { 
     if (ProgressUpdate != null) 
     ProgressUpdate(this, new YourEventArgs { myData = (string)input }); 
     } 

     //calculation method 
     public void FlushData() 
     { 
      MessageBox.Show("this is a test !"); 
      update("test"); 
     } 
    } 

    public class YourEventArgs : EventArgs 
    { 
     public string myData { get; set; } 
    } 
} 

感謝您的幫助。

+0

相關,可能重複:http://stackoverflow.com/questions/11625208/accessing-ui-main-thread-safely- in-wpf – jdphenix

+0

如果在.net 4.0中有任務,可以通過當前同步上下文在? – VMAtm

+0

我剛開始時使用了這個功能,但後來爲了ReportProgress功能而轉移到了BGW,最後你是對的,這不是更好,當我解決這個問題時,我會切換回任務。 – Coloris

回答

0

您可以簡單地從FlushData()方法中調用ProgressUpdate事件。

只需撥打:

If (ProgressUpdate !=null) 
{ 
    ProgressUpdate(this,new YourEventArgs()) 
} 

this是源實例,其中event源於。

您可以通過繼承EventArgs類來創建YourEventArgs類。

public class YourEventArgs : EventArgs 
    { 
    //Put any property that you want to pass back to UI here. 
    } 

event在UI被提出:

RaiseEvent.ProgressUpdate += (s, e) => 
     { 
      Dispatcher.BeginInvoke((Action)(() => 
             { 
              lstTest.Items.Add("this is a test : "); 
              //Add items to your UI control here... 
             })); 
     }; 

Ë將YourEventArgs類型。

在附註上,你不應該碰到不同線程的UI線程(如你的例子中的後臺工作線程)。由於您的event-handler已經做了Dispatcher.BeginInvoke,這是安全的。

此外,您的ProgressUpdate事件應該在您的類SetXmlData中。

+0

嗨ANewGuyInTown,並感謝你的答覆。在閱讀您的解決方案後,我更新了我的代碼。 到目前爲止,我不得不在DoWork方法內部移動訂閱,如果我不這樣做,處理程序總是被設置爲null,並且事件不會被引發?!是否因爲我需要從用於實例化SetXmlData類的線程來完成它? 此外,我無法從myData屬性中獲取內容,當我嘗試使用evt.myData時,我有: 'System.EventArgs'不包含'myData'的定義,也沒有擴展方法'myData'接受'System.EventArgs'類型的第一個參數可以被發現 – Coloris

+0

修改你的'ProgressUpdated'事件hanlder爲'公共事件EventHandler ProgressUpdate;'是的,你必須在實例化'SetXmlData'的線程中執行它類。 – ANewGuyInTown

+0

它的工作原理!再次謝謝 – Coloris

0

嘗試get; set;例如:

Form1中:

public partial class Form1 : Form 
{ 
    static public string gettext { get; set; } 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Class1.send(); //call to class function 
     textBox1.Text = gettext; //items.add(gettext) 
    } 
} 

的Class1:

class Class1 
{ 
    static public void send() 
    { 
     Form1.gettext = "Marko"; //Set gettext to string "Marko" 

    } 
} 
相關問題