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. C#
  4. Another thread question

Another thread question

Scheduled Pinned Locked Moved C#
csharpquestionmobiledatabase
5 Posts 2 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.
  • P Offline
    P Offline
    PaleyX
    wrote on last edited by
    #1

    My app was never designed to support threading - The GUI has a VB6 front end but I have rewritten it in VB.NET with a C# backend - don't ask :wtf: Some of the operations in the C# back end can take a long time are thus good candidates for running on a worker thread - these operations communicate back to the GUI using events - status information, adding rows to a datagrid, progress bar etc. As I am new to threads in .NET can I still use the events from the function running on the worker thread but just change the event sinks so they use BeginInvoke to update the GUI controls? The class which contains the long running operations has some database code in it - can I call that code from the long running operation on the new thread just as I would if it wasn't running on the thread? I don't believe I have any synchronisation issues as the database is always read only - the only reason I want the long running operation to run on a separate thread is so the GUI remains responsive, I dont actually intend to let the user do anything with the GUI other than maybe cancel the long running operation. TIA

    P 1 Reply Last reply
    0
    • P PaleyX

      My app was never designed to support threading - The GUI has a VB6 front end but I have rewritten it in VB.NET with a C# backend - don't ask :wtf: Some of the operations in the C# back end can take a long time are thus good candidates for running on a worker thread - these operations communicate back to the GUI using events - status information, adding rows to a datagrid, progress bar etc. As I am new to threads in .NET can I still use the events from the function running on the worker thread but just change the event sinks so they use BeginInvoke to update the GUI controls? The class which contains the long running operations has some database code in it - can I call that code from the long running operation on the new thread just as I would if it wasn't running on the thread? I don't believe I have any synchronisation issues as the database is always read only - the only reason I want the long running operation to run on a separate thread is so the GUI remains responsive, I dont actually intend to let the user do anything with the GUI other than maybe cancel the long running operation. TIA

      P Offline
      P Offline
      PaleyX
      wrote on last edited by
      #2

      Sorry, posted it twice - the first attempt too 20 minutes then went blank - when I looked it wasn't there so I reposted it - naturally the first one decided to appear in the meantime :mad: Rugby League: The Greatest Game Of All.

      T 1 Reply Last reply
      0
      • P PaleyX

        Sorry, posted it twice - the first attempt too 20 minutes then went blank - when I looked it wasn't there so I reposted it - naturally the first one decided to appear in the meantime :mad: Rugby League: The Greatest Game Of All.

        T Offline
        T Offline
        TylerBrinks
        wrote on last edited by
        #3

        Multithreading w/ a GUI on MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp[^]

        P 1 Reply Last reply
        0
        • T TylerBrinks

          Multithreading w/ a GUI on MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp[^]

          P Offline
          P Offline
          PaleyX
          wrote on last edited by
          #4

          Thanks, I have read those articles and also in his book on Windows Forms programming where the articles are repeated. They don't really address my events question though.

          T 1 Reply Last reply
          0
          • P PaleyX

            Thanks, I have read those articles and also in his book on Windows Forms programming where the articles are repeated. They don't really address my events question though.

            T Offline
            T Offline
            TylerBrinks
            wrote on last edited by
            #5

            Yes, you can still use evens. The article details exactly how to do it in terms of your GUI. First, subscribe to the events or use a delegate which points to your GUI's method. Either way will work fine. Your thread will call it's delegate or raise its event, and the UI thread will receive the notification. In your gui you'll need code similar to this to handle the event:

            private delegate void uiThreadDelegate(object sender, EventArgs e);

            protected void MyEventHandler(object sender, EventArgs e)
            {
            if(this.InvokeRequired)
            {
            uiThreadDelegate ui = new ThreadDelegate(MyEventHandler);
            this.BeginInvoke(ui, new object[]{sender, e});
            }else{
            ... your code
            }
            }

            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