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. Updating a progressbar from a seperate thread

Updating a progressbar from a seperate thread

Scheduled Pinned Locked Moved C#
helpquestionannouncement
10 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.
  • S Offline
    S Offline
    sharpiesharpie
    wrote on last edited by
    #1

    My program uses multiple threads to do something, and i want to make a progress bar to show the process status, the problem is that i can't update the progressbar from the thread (gives an exception..not the thread it was created on). what can i do?

    L 1 Reply Last reply
    0
    • S sharpiesharpie

      My program uses multiple threads to do something, and i want to make a progress bar to show the process status, the problem is that i can't update the progressbar from the thread (gives an exception..not the thread it was created on). what can i do?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Use the standard technique (InvokeRequired/Invoke) explained over and over in CP articles; both of my articles contain a simple example. :)

      Luc Pattyn [My Articles]

      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Use the standard technique (InvokeRequired/Invoke) explained over and over in CP articles; both of my articles contain a simple example. :)

        Luc Pattyn [My Articles]

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

        Uhh...well...I read a bit and it seems to be really confusing...a small simplified example would be nice :\

        L 1 Reply Last reply
        0
        • S sharpiesharpie

          Uhh...well...I read a bit and it seems to be really confusing...a small simplified example would be nice :\

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, the last code snippet in my Sokoban article shows the simplest example possible: executing a method with one argument on the UI thread (it is an excerpt from file MainForm.cs) :)

          Luc Pattyn [My Articles]

          S 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, the last code snippet in my Sokoban article shows the simplest example possible: executing a method with one argument on the UI thread (it is an excerpt from file MainForm.cs) :)

            Luc Pattyn [My Articles]

            S Offline
            S Offline
            sharpiesharpie
            wrote on last edited by
            #5

            Uhh...i don't get it (especially cause I'm not very familiar with delegates...so can you explain what this does: Invoke(new MovePaster(pasteOneMove), new object[]{c}); You send a function into a delegate? but the function is void and has a char as a parameter (And so does the delegate)...so...uhh...I'm confused :\

            L 1 Reply Last reply
            0
            • S sharpiesharpie

              Uhh...i don't get it (especially cause I'm not very familiar with delegates...so can you explain what this does: Invoke(new MovePaster(pasteOneMove), new object[]{c}); You send a function into a delegate? but the function is void and has a char as a parameter (And so does the delegate)...so...uhh...I'm confused :\

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, whenever you create a Win app in .NET you use delegates; how else would you connect an event handler (e.g. myButton_Click) to the corresponding event ? When you write

              myButton.Click+=new EVentHandler(myButton_Click);

              you pass a method to the handler's constructor; this does not call myButton_Click, it tells the button that when clicked it will have to run myButton_Click for this object. This is fundamental to .NET, whether using C# or not. Invoke is similar, the line

              Invoke(new MovePaster(pasteOneMove), new object[]{c});

              will cause the execution of the specified method (with specified argument list), so it logically corresponds to a simple pasteOneMove(c); but it does not execute the method on the calling thread, it sends a message to the message pump and that message causes pasteOneMove(c); to be executed on the UI thread. A delegate is used gfor this; it is an object used to combine the relevant object and method in a structured (type-safe) way. I recommend you read more about this in any .NET programmers book, then have a look at some of the many examples around. :)

              Luc Pattyn [My Articles]

              S 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, whenever you create a Win app in .NET you use delegates; how else would you connect an event handler (e.g. myButton_Click) to the corresponding event ? When you write

                myButton.Click+=new EVentHandler(myButton_Click);

                you pass a method to the handler's constructor; this does not call myButton_Click, it tells the button that when clicked it will have to run myButton_Click for this object. This is fundamental to .NET, whether using C# or not. Invoke is similar, the line

                Invoke(new MovePaster(pasteOneMove), new object[]{c});

                will cause the execution of the specified method (with specified argument list), so it logically corresponds to a simple pasteOneMove(c); but it does not execute the method on the calling thread, it sends a message to the message pump and that message causes pasteOneMove(c); to be executed on the UI thread. A delegate is used gfor this; it is an object used to combine the relevant object and method in a structured (type-safe) way. I recommend you read more about this in any .NET programmers book, then have a look at some of the many examples around. :)

                Luc Pattyn [My Articles]

                S Offline
                S Offline
                sharpiesharpie
                wrote on last edited by
                #7

                So if i want to update my progress bar...i need to do something like this? private delegate void SetPrecentage(int p); private int DoOperation(int p) { if (this.InvokeRequired){ Invoke(new SetPrecentage(DoOperation),new object[]{p});} } but then...how do i like, update the progress bar?

                S 1 Reply Last reply
                0
                • S sharpiesharpie

                  So if i want to update my progress bar...i need to do something like this? private delegate void SetPrecentage(int p); private int DoOperation(int p) { if (this.InvokeRequired){ Invoke(new SetPrecentage(DoOperation),new object[]{p});} } but then...how do i like, update the progress bar?

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #8

                  Do that in the else block.

                  private int DoOperation(int p)
                  {
                  if (this.InvokeRequired)
                  {
                  Invoke(new SetPrecentage(DoOperation),new object[]{p});}
                  }
                  else
                  {
                  // The else block runs on the UI thread, so can update the progressbar safely.
                  }

                  Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                  S 1 Reply Last reply
                  0
                  • S S Senthil Kumar

                    Do that in the else block.

                    private int DoOperation(int p)
                    {
                    if (this.InvokeRequired)
                    {
                    Invoke(new SetPrecentage(DoOperation),new object[]{p});}
                    }
                    else
                    {
                    // The else block runs on the UI thread, so can update the progressbar safely.
                    }

                    Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                    S Offline
                    S Offline
                    sharpiesharpie
                    wrote on last edited by
                    #9

                    but i mean, it will never run on a UI thread, so how do i update the progressbar? what will invoking p do? :\

                    S 1 Reply Last reply
                    0
                    • S sharpiesharpie

                      but i mean, it will never run on a UI thread, so how do i update the progressbar? what will invoking p do? :\

                      S Offline
                      S Offline
                      S Senthil Kumar
                      wrote on last edited by
                      #10

                      The else code runs on the UI thread. When you call BeginInvoke or Invoke and pass it a delegate, .NET calls the delegate on the UI thread. In your example, you passed the same function as the delegate. When .NET calls the function on the UI thread, InvokeRequired will be false and the else code will run on the UI thread. Have a look at What's up with BeginInvoke[^] to know more about this.

                      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                      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