2

I created new Window on a different thread because it has heavy UI operations, that was done to keep my main window run smoothly. Everything works perfectly.

But here is the question:
How I can access newly created window?
After calling Dispatcher.Run() I can not manipulate visualisationWindow anymore. I want to keep access to that newly created window object.

Here is how my window created:

 private void CreateVisualisationWindow() { Thread VisualisationWIndowThread = new Thread(new ThreadStart(ThreadStartingPoint)); VisualisationWIndowThread.SetApartmentState(ApartmentState.STA); VisualisationWIndowThread.IsBackground = true; VisualisationWIndowThread.Start(); } private void ThreadStartingPoint() { Visualisation visualisationWindow = new Visualisation(); visualisationWindow.Show(); System.Windows.Threading.Dispatcher.Run(); } 

Also I tried accessing it through System.Windows.Threading.Dispatcher.FromThread(VisualisationWIndowThread) but seems I misunderstand some core things.

3
  • Using multiple ui threads is one of those things you can do technically BUT is almost always a bad idea. I think a different approach with one ui thread is probably a better plan. What does your visualisation window actually do? As an alternative, why do you need to access the actual window? I would expect to work with some sort of UI agnostic viewmodel which CAN deal with multiple threads. Commented Dec 5, 2018 at 10:09
  • @Andy, there are multiple ui threads only because second window includes WinForms element that using UI thread heavily (screenBG.Render() where screenBG is of BufferedGraphics type - that mehtod can only be executed on UI thread) and there is no way to avoid it even with async approach. It is used to display some science data Commented Dec 5, 2018 at 10:48
  • I'm not familiar with screenbg.render. Do you have to use that? I was thinking maybe writeablebitmap. Process on a backround thread, translate to bitmap, freeze and pass back. Although.... Just building a wbm is very fast so long as you free up the ui thread a little between changes you can construct very quickly changing images. Commented Dec 5, 2018 at 19:31

1 Answer 1

2

I simulated your issue using two WPF Window objects and a timer to ensure that the Second Window was created before calling operations on it. Below is my code sample and it updates the second Windows TextBox every five seconds:

 private Timer _timer; private SecondWindow _secondWindow; public MainWindow() { InitializeComponent(); CreateVisualisationWindow(); _timer = new Timer(Callback); _timer.Change(5000, 5000); } private void Callback(object state) { UpdateSecondWindowText(); } private void CreateVisualisationWindow() { Thread VisualisationWIndowThread = new Thread(ThreadStartingPoint); VisualisationWIndowThread.SetApartmentState(ApartmentState.STA); VisualisationWIndowThread.IsBackground = true; VisualisationWIndowThread.Start(); } private void ThreadStartingPoint() { _secondWindow = new SecondWindow(); _secondWindow.SecondWindowTextBlock.Text = "Hello"; _secondWindow.Show(); Dispatcher.Run(); } private void UpdateSecondWindowText() { _secondWindow.Dispatcher.BeginInvoke(new Action(() => { _secondWindow.SecondWindowTextBlock.Text = _secondWindow.SecondWindowTextBlock.Text + " World"; })); } 

So the trick is, you need to call the Dispatcher on the second Window in order to gain access to it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.