2012-01-02 35 views
1

我有計劃像Start_method_2使用該線程在WPF

private void do_my_method_click(object sender, RoutedEventArgs e) 
{ 
     //there are some variables and methods here 

     //works fine 
     ThreadPool.QueueUserWorkItem(Start_method); 

     // when added gives error this thread owned by other thread 
     ThreadPool.QueueUserWorkItem(Start_method_2); 
} 

Start_method(object state) 
{ 
} 

Start_method_2(object state) 
{ 
} 

Start_method輸出我不知道究竟在何處我錯了,我爲WPF和C#中的新手。

+2

「給出錯誤這個線程被其他線程擁有」請檢查此,張貼_exact_錯誤信息(在不一個評論) – 2012-01-02 12:08:00

+3

普通人,放輕鬆,他說他是新人。給他一些建設性的反饋。 – oleksii 2012-01-02 12:11:37

回答

1

由於@Tudor建議,在Start_method_2之內,您正在修改我認爲的GUI。

如果您正在UI上修改主線程中的某些內容,請使用System.Threading.SynchronizationContext.Current。這裏是一個例子:

var sync = System.Threading.SynchronizationContext.Current; 

sync.Post(x => { 

    TextBlock1.Text = "Foo"; 

}, null); 

這段代碼是安全的,但它錯過了很多(異常處理等)。這也是當我知道穿一點,我遭遇了類似的問題,另外一個問題:

Simple Async operation with Continuation scenario doesn't work on a WPF app

0

如果方法1的輸出在第二次使用,則存在競爭條件的危險。

該異常可能是由於訪問worker中的UI控件而導致的。向我們展示方法,以便我們提供更詳細的解決方案。