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