I have a options window and a window displaying color based on these options and Kinect data. So far, everything is on one thread (as far as I know, I did not make threads).
Now I am adding the ability to open a viewport, which will need to be updated with the minimum possible delay. All this entails the creation of a window and its display:
viewer = new SkeletalViewer.MainWindow(); viewer.Show();
When this event fires, the color window stops displaying colors (that is, an event that fires 30 times per second on the main stream stops shooting), but the viewer displays perfectly. I want the viewer and color window to be updated.
From reading other questions, the solution seems to be to create a viewer in a new stream. However, I am facing a lot of problems.
This fires when I click the button to open the viewer:
private void launchViewerThread_Click(object sender, RoutedEventArgs e) { Thread viewerThread = new Thread(delegate() { viewer = new SkeletalViewer.MainWindow(); viewer.Dispatcher.Invoke(new Action(delegate() { viewer.Show(); })); }); viewerThread.SetApartmentState(ApartmentState.STA);
Regardless of whether I simply call viewer.Show () or Invoke () as above, the line throws an exception: You cannot use a DependencyObject object that belongs to a thread other than its parent Freezable. This is how I understand Invoke (): it calls the view manager, which knows which thread the object is working on, and can then call methods from that stream.
Should I try to put this viewer in a new stream? Is the problem even a matter of threads? The user will not interact with the viewer.
Does anyone know why this is not working? Thanks for the help.
multithreading c # invoke
michael.greenwald
source share