The calling thread cannot access this object because a different thread owns it.WPF

后端 未结 6 833
一向
一向 2020-11-28 10:16

Whenever I refresh a label, I got this error: The calling thread cannot access this object because a different thread owns it. I tried to invoke b

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 10:39

    You can use Dispatcher for this. Your code becomes...

    private void imgPayment_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() =>
        {
            try
            {
                label1.Content = "df";
            }
            catch
            {
                lostfocs ld = new lostfocs(up);
              //  ld.Invoke("df");
                object obj=new object();
                ld.Invoke("sdaf");
            }
        }
    ));
    

提交回复
热议问题