调用线程无法访问对象,但调度器.invoke无法解决问题 [英] Calling thread cannot access the object but dispachter .invoke is not solving the problem

查看:105
本文介绍了调用线程无法访问对象,但调度器.invoke无法解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从kinect拍摄视频并发送到客户端,但我遇到的问题是它说调用线程无法访问对象不同线程拥有它我使用dispachter.invoke但它给了我相同的异常而不显示要求的结果。

I am taking Video from kinect and sending to client but the problem i am facing is that it says calling thread cannot access the object different threads owns it i use dispachter.invoke but it is giving me the same exception and not displaying the required result.

public partial class MainWindow : Window
   {
       TcpClient client;
       NetworkStream ns;
       Thread vedioframe;
       WriteableBitmap vediofram = null;

       public MainWindow()
       {
           InitializeComponent();
           client = new TcpClient();
           client.Connect("127.0.0.1", 9000);
           vedioframe = new Thread(Display_Frame);
          vedioframe.Start();
       }
       public void Display_Frame()
       {

           ns = client.GetStream();


           while (true)
           {
               byte[] vedio = new byte[1228800];
               ns.Read(vedio, 0, vedio.Length);
               try
               {
                   if (vediofram == null)
                   {
                       vediofram = new WriteableBitmap(640, 480, 96, 96, PixelFormats.Bgr32, null);

                  }
                  else
                   {

                       vediofram.WritePixels(new Int32Rect(0, 0, 640, 480), vedio, 640 * 4, 0);
                  }
                   Update_Frame(vediofram);

               }
               catch (Exception e)
               {
                   MessageBox.Show(e.Message);
               }

          }
       }
       void Update_Frame(WriteableBitmap src)
       {
           Dispatcher.Invoke(new Action(() => { Vedio.Source = src; }));
       }

   }



显示帧功能是一个Seprate线程,从该线程调用Update_frame来更新视频frame


Display Frame function is a Seprate Thread and from that thread Update_frame is called to update video frame

推荐答案

你好阿里,



如果你知道的话,似乎是一个简单的解决方案。



您无法从另一个线程(而不是创建它的线程)访问WriteableBitmap。如果你想这样做,你需要先调用
Hi ali,

Seems a simple solution if you know it.

You can't access the WriteableBitmap from another thread (not the thread created it). If you want to do that, you need to freeze your bitmap by calling
WriteableBitmap.Freeze()

来冻结你的位图。



并且您无法在不是调度员线程的线程中访问... .Source



我想在给Freeze打电话之后你必须为下一帧创建一个新的图像(不是那个就好了)



这有帮助吗?



亲切的问候Johannes

first.

And You can't access ....Source in a thread that is not the dispatcher thread.

I think after call to Freeze you will have to create a new Image for the next frame (not shure on that)

Does this help?

Kind regards Johannes


这篇关于调用线程无法访问对象,但调度器.invoke无法解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆