C# WPF中的线程

C# WPF中的线程,c#,wpf,multithreading,thread-safety,threadpool,C#,Wpf,Multithreading,Thread Safety,Threadpool,我有这样的节目 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 owne

我有这样的节目

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_方法的输出
用于
Start_方法_2
我不知道我到底哪里出了问题,我是WPF和C的新手。

如果方法1的输出在第二秒使用,您将面临竞争条件的危险


该异常可能是由于访问工作进程中的UI控件引起的。向我们展示方法,以便我们提供更详细的解决方案。

如果方法1的输出在第二秒使用,您将面临竞争条件的危险


该异常可能是由于访问工作进程中的UI控件引起的。向我们展示方法,以便我们提供更详细的解决方案。

正如@Tudor所建议的,在
Start\u method\u 2
中,我认为您正在修改GUI

如果要修改在主线程上工作的UI上的某些内容,请使用
System.Threading.SynchronizationContext.Current
。以下是一个例子:

var sync = System.Threading.SynchronizationContext.Current;

sync.Post(x => { 

    TextBlock1.Text = "Foo";

}, null);
这段代码是安全的,但它遗漏了很多(异常处理等)。这里还有一个类似问题的另一个问题,当我了解一点线程时,我遇到了这个问题:


正如@Tudor所建议的,在
Start\u方法2
中,我认为您正在修改GUI

如果要修改在主线程上工作的UI上的某些内容,请使用
System.Threading.SynchronizationContext.Current
。以下是一个例子:

var sync = System.Threading.SynchronizationContext.Current;

sync.Post(x => { 

    TextBlock1.Text = "Foo";

}, null);
这段代码是安全的,但它遗漏了很多(异常处理等)。这里还有一个类似问题的另一个问题,当我了解一点线程时,我遇到了这个问题:


“将此线程归其他线程所有”请检查此项,发布准确的错误消息(不在评论中)普通人,对他放轻松,他说他是新来的。给他一些建设性的反馈。“给错误这个线程属于其他线程”请检查这个,张贴准确的错误消息(不是在评论中)普通人,对他放松,他说他是新的。给他一些建设性的反馈。