2012-01-02 60 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控件而导致的。向我们展示方法,以便我们提供更详细的解决方案。