Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Multithreaded Windows with Data Binding

Multithreaded Windows with Data Binding

Scheduled Pinned Locked Moved WPF
wpfwcfsysadminannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominick Marciano
    wrote on last edited by
    #1

    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 Sub

    And the window's constructor is:

    Public Sub New(properties As WindowProperties)
    InitializeComponent()
    _ticket = properties.Ticket
    Me.Top = properties.Top
    SetupWindow()
    End Sub

    And the SetupWindow

    A S 2 Replies Last reply
    0
    • D Dominick Marciano

      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 Sub

      And the window's constructor is:

      Public Sub New(properties As WindowProperties)
      InitializeComponent()
      _ticket = properties.Ticket
      Me.Top = properties.Top
      SetupWindow()
      End Sub

      And the SetupWindow

      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      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 !~

      1 Reply Last reply
      0
      • D Dominick Marciano

        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 Sub

        And the window's constructor is:

        Public Sub New(properties As WindowProperties)
        InitializeComponent()
        _ticket = properties.Ticket
        Me.Top = properties.Top
        SetupWindow()
        End Sub

        And the SetupWindow

        S Offline
        S Offline
        SledgeHammer01
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups