I am trying to give some user entertainment, and show a "please wait" window, with Marquee, during the loading of a separate complex Window. I am attempting to do this by loading the Window in a new thread, like this:
Public Function ShowPleaseWait() As System.Threading.Thread
Dim PleaseWait As New System.Threading.Thread(AddressOf LoadPleaseWait)
PleaseWait.SetApartmentState(System.Threading.ApartmentState.STA)
PleaseWait.IsBackground = True
PleaseWait.Start()
Return PleaseWait
End Function
Public Sub LoadPleaseWait()
Dim window As New windowPleaseWait
Try
window.Show()
System.Windows.Threading.Dispatcher.Run()
Catch e As System.Threading.ThreadAbortException
window.Close()
window = Nothing
End Try
End Sub
In the calling code, it calls ShowPleaseWait
and saves the Thread for later.. To close the window, it calls Thread.Abort
, on the saved thread. This in turn will causes it to enter the Catch
. I have tried, many different ways, with and without the catch.
This works incredibly, the first time it is called. However, additional calls will fail at window.Show()
with the exception: The calling thread cannot access this object because a different thread owns it.
.
This really puzzles me as the window was created one line above the call to window.Show
and is local. How is it owned by a different thread? How can I fix this?