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. multi threading & progressBar problem

multi threading & progressBar problem

Scheduled Pinned Locked Moved C#
helpquestion
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.
  • G Offline
    G Offline
    gaby2r
    wrote on last edited by
    #1

    public void task() { Thread.Sleep(9000); } private void button_Click(object sender, System.EventArgs e) { this.progressBar1.Value=0; Thread t=new Thread(new ThreadStart(task)); t.Start(); while((t.ThreadState & (System.Threading.ThreadState.Stopped | System.Threading.ThreadState.Unstarted)) == 0) { if(this.progressBar1.Value==this.progressBar1.Maximum) { this.progressBar1.Value=this.progressBar1.Minimum; } this.progressBar1.PerformStep(); Thread.Sleep(500); } t.Join(); } works the first time, progressbar progresses nice and neat, never works again. what am i missing here?

    D S 2 Replies Last reply
    0
    • G gaby2r

      public void task() { Thread.Sleep(9000); } private void button_Click(object sender, System.EventArgs e) { this.progressBar1.Value=0; Thread t=new Thread(new ThreadStart(task)); t.Start(); while((t.ThreadState & (System.Threading.ThreadState.Stopped | System.Threading.ThreadState.Unstarted)) == 0) { if(this.progressBar1.Value==this.progressBar1.Maximum) { this.progressBar1.Value=this.progressBar1.Minimum; } this.progressBar1.PerformStep(); Thread.Sleep(500); } t.Join(); } works the first time, progressbar progresses nice and neat, never works again. what am i missing here?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You'd be better off encapsulating your ProcessBar code into it's own seperate control. Something that includes it's own Timer to increment itself. You could than use this control in whatever project you wanted and you wouldn't have to worry about coding some custom garbage like what you posted. Take a look at this[^] article on MSDN for an example. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      1 Reply Last reply
      0
      • G gaby2r

        public void task() { Thread.Sleep(9000); } private void button_Click(object sender, System.EventArgs e) { this.progressBar1.Value=0; Thread t=new Thread(new ThreadStart(task)); t.Start(); while((t.ThreadState & (System.Threading.ThreadState.Stopped | System.Threading.ThreadState.Unstarted)) == 0) { if(this.progressBar1.Value==this.progressBar1.Maximum) { this.progressBar1.Value=this.progressBar1.Minimum; } this.progressBar1.PerformStep(); Thread.Sleep(500); } t.Join(); } works the first time, progressbar progresses nice and neat, never works again. what am i missing here?

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

        I don't know how it works the first time. You are blocking the UI thread by looping till the worker thread completes. So your calls to PerformStep won't really get through till button_Click completes, by which time you're already done. I'd suggest that you update the progress bar from the worker thread (by using BeginInvoke/Invoke as this[^] article describes. Regards Senthil _____________________________ My Blog | My Articles | 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