Click here to Skip to main content
15,868,098 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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; }));
       }

   }

Display Frame function is a Seprate Thread and from that thread Update_frame is called to update video frame
Posted
Updated 26-Jun-13 21:01pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Jun-13 16:36pm    
It's impossible to give a final answer because your code is not comprehensive. Where, for example "vedio" (not "Vedio") is declared? initialized? The problem is: you should work with that object in one thread, not in two or more; and you are mixing access from different threads. Why, for example, ns.Read(vedio, 0, vedio.Length); is accessed from the same thread as Dispatcher.Invoke (which provides access to Vedio from UI thread)? In what threads do you access vedio and in what threads — Vedio?
—SA
ali 10 26-Jun-13 4:41am    
public MainWindow()
{
InitializeComponent();
client = new TcpClient();
client.Connect("127.0.0.1", 9000);
vedioframe = new Thread(Display_Frame);
vedioframe.Start();
WriteableBitmap vediofram = null;
}
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; }));
}
this is the complete code i am receiving the video buffer and displaying it.The function display_frame() is a separate thread and this function is responsible for displaying video and Update_frame function is giving the error.Vedio is image control and vedio is a buffer as initialized in while loop
Sergey Alexandrovich Kryukov 26-Jun-13 9:53am    
Can you avoid putting code in comments? Will you use "Improve question" instead?
—SA

1 solution

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()
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
 
Share this answer
 
Comments
ali 10 28-Jun-13 16:20pm    
i have created WritableBitmap in the dispatcher thread but it results in flickering of video i don't no why?? any idea about it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900