Multithreaded Windows with Data Binding
-
I am working on an application that will need to open multiple windows, each on a separate thread. I have each on a different thread because each window represents a "Ticket" which will need to get data from a server, update a server, as well as other stuff and when all were running on a single thread some of the ticket windows were lagging. I am able to create the ticket windows and pass arguments, however as soon as I start trying to update properties on the window I get exceptions. Currently each window that needs to be created is created using this method:
Private Shared Sub CreateNewWindow(Of T As Window)(ByVal newThread As Boolean, ByVal onOpened As Action(Of T), ByVal ParamArray args() As Object)
Dim activateWindow As Action = Sub()
Dim window As T = DirectCast(Activator.CreateInstance(GetType(T), args), T)
AddHandler window.Closed, Sub(s, e)
window.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Background)
End Sub
window.Show()
If onOpened IsNot Nothing Then
onOpened(window)
End If
End Sub
If newThread Then
Dim newWindowThread As New Thread(New ThreadStart(Sub()
SynchronizationContext.SetSynchronizationContext(New DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher))
activateWindow()
System.Windows.Threading.Dispatcher.Run()
End Sub))
newWindowThread.SetApartmentState(ApartmentState.STA)
newWindowThread.IsBackground = True
newWindowThread.Start()
Else
activateWindow()
End If
End SubAnd the window's constructor is:
Public Sub New(properties As WindowProperties)
InitializeComponent()
_ticket = properties.Ticket
Me.Top = properties.Top
SetupWindow()
End SubAnd the
SetupWindow
-
I am working on an application that will need to open multiple windows, each on a separate thread. I have each on a different thread because each window represents a "Ticket" which will need to get data from a server, update a server, as well as other stuff and when all were running on a single thread some of the ticket windows were lagging. I am able to create the ticket windows and pass arguments, however as soon as I start trying to update properties on the window I get exceptions. Currently each window that needs to be created is created using this method:
Private Shared Sub CreateNewWindow(Of T As Window)(ByVal newThread As Boolean, ByVal onOpened As Action(Of T), ByVal ParamArray args() As Object)
Dim activateWindow As Action = Sub()
Dim window As T = DirectCast(Activator.CreateInstance(GetType(T), args), T)
AddHandler window.Closed, Sub(s, e)
window.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Background)
End Sub
window.Show()
If onOpened IsNot Nothing Then
onOpened(window)
End If
End Sub
If newThread Then
Dim newWindowThread As New Thread(New ThreadStart(Sub()
SynchronizationContext.SetSynchronizationContext(New DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher))
activateWindow()
System.Windows.Threading.Dispatcher.Run()
End Sub))
newWindowThread.SetApartmentState(ApartmentState.STA)
newWindowThread.IsBackground = True
newWindowThread.Start()
Else
activateWindow()
End If
End SubAnd the window's constructor is:
Public Sub New(properties As WindowProperties)
InitializeComponent()
_ticket = properties.Ticket
Me.Top = properties.Top
SetupWindow()
End SubAnd the
SetupWindow
I am unsure whether the problem was generated from these objects, or from any other underlying objects inside your code. I remember, I once had the same problem and the Dispatcher worked for me. But I would like to advise, that the second exception is raised when there was a resource being used and another thread attempts to use it too. In that case, it throws this exception. So, a tip might be give to you, you must always try to use the Dispose() method to dispose all of the resources and release them before closing your applications. This would minimize the second exceptions. Or even if you don't remember calling this method on every dispose-able object. Them you might consider using such objects under a
using
block. Which will let the .NET take care of these resources once you're done working with them.The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
I am working on an application that will need to open multiple windows, each on a separate thread. I have each on a different thread because each window represents a "Ticket" which will need to get data from a server, update a server, as well as other stuff and when all were running on a single thread some of the ticket windows were lagging. I am able to create the ticket windows and pass arguments, however as soon as I start trying to update properties on the window I get exceptions. Currently each window that needs to be created is created using this method:
Private Shared Sub CreateNewWindow(Of T As Window)(ByVal newThread As Boolean, ByVal onOpened As Action(Of T), ByVal ParamArray args() As Object)
Dim activateWindow As Action = Sub()
Dim window As T = DirectCast(Activator.CreateInstance(GetType(T), args), T)
AddHandler window.Closed, Sub(s, e)
window.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Background)
End Sub
window.Show()
If onOpened IsNot Nothing Then
onOpened(window)
End If
End Sub
If newThread Then
Dim newWindowThread As New Thread(New ThreadStart(Sub()
SynchronizationContext.SetSynchronizationContext(New DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher))
activateWindow()
System.Windows.Threading.Dispatcher.Run()
End Sub))
newWindowThread.SetApartmentState(ApartmentState.STA)
newWindowThread.IsBackground = True
newWindowThread.Start()
Else
activateWindow()
End If
End SubAnd the window's constructor is:
Public Sub New(properties As WindowProperties)
InitializeComponent()
_ticket = properties.Ticket
Me.Top = properties.Top
SetupWindow()
End SubAnd the
SetupWindow
As discussed in your other thread, all GUI work needs to be done on the main thread. You need to restructure your code so that the *work* for each window is done in a background thread (easy with the async keyword or TPL for example) so it doesn't block the UI. Also, bindings should generally be lazy loaded. If they are expensive or time consuming, you should make them async or background. You will encounter endless hurdles creating GUI objects on background threads.