Florin
Florin

Reputation: 23

make a window running on one thread modal to main window running on another one

I have the Mainwindow running on its own UI single STA thread. AT some point in the application I am running, for limited time, another window on a different single STA thread. The problem I am having is that the second tread runs on a different location on the screens, which outside the boundaries of the MainWindow. I would like to re-position the second thread over the MainWindow.

Thank you in advance!

Upvotes: 0

Views: 64

Answers (1)

Florin
Florin

Reputation: 23

Found myself the solution. I wanted to put the second thread right in the center of UI thread (first thread). I had to calculate the desired location in the UI thread, and pass it as an argument to a Sub in the second thread, and use it after showing the window and Start/Run the second thread.

Getting the location in the first thread:

Dim WithEvents MySpinner As SpinWindow = New SpinWindow
....
Dim intL = ActualWidth
Dim intH = ActualHeight
myLocation = MainWindowBorder.PointToScreen(New Windows.Point(0, 0))
myLocation.X = myLocation.X + (intL / 2) + 60
myLocation.Y = myLocation.Y + (intH / 2)

Passing the location as argument:

MySpinner.OnCreateNewWindow(myLocation)

In the second thread:

Public Class SpinWindow

Dim myThread As Thread
Dim w As SpinWindow
Public Sub OnCreateNewWindow(loc As Windows.Point)
    myThread = New Thread(Sub()
                              w = New SpinWindow()
                              w.TheSpin.Visibility = Visibility.Visible
                              w.Show()
                          --> w.Left = loc.X
                          --> w.Top = loc.Y
                              AddHandler w.Closed, AddressOf w.Dispatcher.InvokeShutdown
                              System.Windows.Threading.Dispatcher.Run()
                          End Sub)
    myThread.SetApartmentState(ApartmentState.STA)
    myThread.IsBackground = True
    myThread.Start()
End Sub

To close the thread, use the following in the first/UI thread:

MySpinner.Dispatcher.InvokeShutDown

Be loved!

Upvotes: 0

Related Questions