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. Visual Basic
  4. Show dataset created in own thread in GUI

Show dataset created in own thread in GUI

Scheduled Pinned Locked Moved Visual Basic
csharpvisual-studiohelpquestion
4 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.
  • J Offline
    J Offline
    Johan Hakkesteegt
    wrote on last edited by
    #1

    Hi, (.net 4, VS 2010 express) I have a windows form with a DataGridView, a dataset, a timer, and a boolean variable (GUI thread). I have a separate thread where I fill another dataset, created in that thread (data thread), when this thread has filled its own dataset I set the boolean to true. At the timer tick event, in the GUI thread, I check the boolean. If it is true, I copy the data thread's dataset to the GUI thread's dataset (MyGUIThreadDataSet = MyDataThreadDataSet.Copy). Then I attach the GUI thread's dataset to the DataGridView (DataGridView.DataSource = MyGUIThreadDataSet). When I run the application, the line DataGridView.DataSource = MyGUIThreadDataSet causes a cross thread error. What am I missing here? Cheers, Johan

    My advice is free, and you may get what you paid for.

    S 1 Reply Last reply
    0
    • J Johan Hakkesteegt

      Hi, (.net 4, VS 2010 express) I have a windows form with a DataGridView, a dataset, a timer, and a boolean variable (GUI thread). I have a separate thread where I fill another dataset, created in that thread (data thread), when this thread has filled its own dataset I set the boolean to true. At the timer tick event, in the GUI thread, I check the boolean. If it is true, I copy the data thread's dataset to the GUI thread's dataset (MyGUIThreadDataSet = MyDataThreadDataSet.Copy). Then I attach the GUI thread's dataset to the DataGridView (DataGridView.DataSource = MyGUIThreadDataSet). When I run the application, the line DataGridView.DataSource = MyGUIThreadDataSet causes a cross thread error. What am I missing here? Cheers, Johan

      My advice is free, and you may get what you paid for.

      S Offline
      S Offline
      Sonhospa
      wrote on last edited by
      #2

      Hi Johan, did you already try the standard method using a delegate? It's commonly used to avoid cross-thread trouble... This is an untested example - just an idea:

      Public Delegate Sub CopyDelegate(ByVal e As CopyEventArgs)

      Public Sub UpdateGui(ByVal e As CopyEventArgs)
      If InvokeRequired Then
      Dim dlg As New CopyDelegate(AddressOf UpdateGui)
      BeginInvoke(dlg, New Object() {e.arg1, e.arg2})
      Else
      ' put everything to happen on the GUI here
      DataGridView.DataSource = MyGUIThreadDataSet
      End If
      End Sub

      Hope it helps :thumbsup: Michael

      modified on Wednesday, May 26, 2010 12:18 PM

      W 1 Reply Last reply
      0
      • S Sonhospa

        Hi Johan, did you already try the standard method using a delegate? It's commonly used to avoid cross-thread trouble... This is an untested example - just an idea:

        Public Delegate Sub CopyDelegate(ByVal e As CopyEventArgs)

        Public Sub UpdateGui(ByVal e As CopyEventArgs)
        If InvokeRequired Then
        Dim dlg As New CopyDelegate(AddressOf UpdateGui)
        BeginInvoke(dlg, New Object() {e.arg1, e.arg2})
        Else
        ' put everything to happen on the GUI here
        DataGridView.DataSource = MyGUIThreadDataSet
        End If
        End Sub

        Hope it helps :thumbsup: Michael

        modified on Wednesday, May 26, 2010 12:18 PM

        W Offline
        W Offline
        William Winner
        wrote on last edited by
        #3

        This is what is needed. The only thing I would note is that you need to make sure the number of parameters and the type of the parameters match.

        BeginInvoke(dlg, New Object() {e.arg1, e.arg2})

        won't work because CopyDelegate only takes one parameter and it's the same type as what UpdateGui takes (as it has to be). I believe it should just be

        BeginInvoke(dlg, New Object() {e})

        Also, you will want to specify which object the InvokeRequired is coming from...as in

        If DataGridView1.InvokeRequired Then
        ...
        'And instead of BeginInvoke
        DataGridView1.BeginInvoke(...)

        J 1 Reply Last reply
        0
        • W William Winner

          This is what is needed. The only thing I would note is that you need to make sure the number of parameters and the type of the parameters match.

          BeginInvoke(dlg, New Object() {e.arg1, e.arg2})

          won't work because CopyDelegate only takes one parameter and it's the same type as what UpdateGui takes (as it has to be). I believe it should just be

          BeginInvoke(dlg, New Object() {e})

          Also, you will want to specify which object the InvokeRequired is coming from...as in

          If DataGridView1.InvokeRequired Then
          ...
          'And instead of BeginInvoke
          DataGridView1.BeginInvoke(...)

          J Offline
          J Offline
          Johan Hakkesteegt
          wrote on last edited by
          #4

          Thank you guys both for your help. Cheers, Johan

          My advice is free, and you may get what you paid for.

          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