How to open a window in a new stream?

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); // needs to be STA or throws exception viewerThread.Start(); } 

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.

+8
multithreading c # invoke
source share
3 answers

You need to call Show() in the same thread in which the window was created - why you get the error. Then you also need to start a new instance of Dispatcher to force runtime to control the window.

 private void launchViewerThread_Click(object sender, RoutedEventArgs e) { Thread viewerThread = new Thread(delegate() { viewer = new SkeletalViewer.MainWindow(); viewer.Show(); System.Windows.Threading.Dispatcher.Run(); }); viewerThread.SetApartmentState(ApartmentState.STA); // needs to be STA or throws exception viewerThread.Start(); } 

See an example. A few examples of Windows / Several threads: http://msdn.microsoft.com/en-us/library/ms741870.aspx

+15
source share

So, I ran into a similar problem when a new window could not be opened in a new thread. The exception was "cannot use a dependent object belonging to another thread."

The problem was that the window was using a global resource (background brush). As soon as I froze the brush resource, the window just loaded.

+1
source share

I'm not sure if this will solve your problem, but you can try creating a proc stream (open a viewport) that runs on another thread, and then send dispatcher.beginInvoke to update the main window.

Here is the code -

  in the constructor register this public MainWindow() { UpdateColorDelegate += UpdateColorMethod; } // delegate and event to update color on mainwindow public delegate void UpdateColorDelegate(string colorname); public event UpdateColorDelegate updateMainWindow; // launches a thread to show viewer private void launchViewerThread_Click(object sender, RoutedEventArgs e) { Thread t = new Thread(this.ThreadProc); t.Start(); } // thread proc public void ThreadProc() { // code for viewer window ... // if you want to access any main window elements then just call DispatchToMainThread method DispatchToUiThread(color); } // private void DispatchToUiThread(string color) { if (updateMainWindow != null) { object[] param = new object[1] { color}; Dispatcher.BeginInvoke(updateMainWindow, param); } } // update the mainwindow control from this method private void UpdateColorMethod(string colorName) { // change control or do whatever with main window controls } 

With this, you can update the controls of the main window without freezing it. Let me know if you have any questions.

0
source share

All Articles