2015-07-22 48 views
0

我在C#初學者,我寫這個代碼啓動新的線程:
如何在c#線程中設置可見值爲真值?

Thread workerThread = new Thread(DoWork); 
workerThread.Priority = ThreadPriority.Highest; 
workerThread.Start(); 


在上行線過程中的一些東西,並顯示到圖表中,每一件事情是好的,但是當!運行並完成DoWork方法,圖表控件可見設置爲false自動,我DoWork方法是:

public void DoWork() 
{ 
    //.....some process and show into the process result into the chart 
    chart1.Visible = true;//this code not run 
} 


該如何解決呢?

+1

什麼項目這是? Windows窗體,WPF,Asp.Net?請在此提供更多代碼。你在哪裏「開始新線程的代碼」位於何處? – Fabjan

+0

Windows窗體,請原諒我忘記 –

+0

是'chart1' UI元素? –

回答

0

更改您的DoWork方法簽名接受對象作爲參數,並通過同步上下文給它:

void DoWork(object o) 
    {   
     SynchronizationContext cont = o as SynchronizationContext; 

     // your logic gere 
     cont.Post(delegate 
     { 
      // all your UI updates here 
     }, null); 
    } 

    Thread workerThread = new Thread(DoWork); 
    workerThread.Priority = ThreadPriority.Highest; 
    workerThread.Start(SynchronizationContext.Current); 
相關問題