Thread Problem Again.
-
I have a windows application with two button and one textarea. the button are start and stop. When I press start I want the program to execute a method x amount of times until I press the stop button. I'm displaying and increment number in the text area and when I run the program it doesn't display anything and it just hands there and I can't click the stop button. How can i fix this and get it to work?
private void startBtn_Click(object sender, System.EventArgs e) { // change enabled - disabled button stopBtn.Enabled = true; startBtn.Enabled = false; StartService(); } private void stopBtn_Click(object sender, System.EventArgs e) { // change enabled - disabled button startBtn.Enabled = true; stopBtn.Enabled = false; StopService(); } public void StartService() { int counter =1; while (start) { status.Text += counter +" test \n"; counter++; Thread.Sleep(5000); } } public void StopService() { start = false; }
-
I have a windows application with two button and one textarea. the button are start and stop. When I press start I want the program to execute a method x amount of times until I press the stop button. I'm displaying and increment number in the text area and when I run the program it doesn't display anything and it just hands there and I can't click the stop button. How can i fix this and get it to work?
private void startBtn_Click(object sender, System.EventArgs e) { // change enabled - disabled button stopBtn.Enabled = true; startBtn.Enabled = false; StartService(); } private void stopBtn_Click(object sender, System.EventArgs e) { // change enabled - disabled button startBtn.Enabled = true; stopBtn.Enabled = false; StopService(); } public void StartService() { int counter =1; while (start) { status.Text += counter +" test \n"; counter++; Thread.Sleep(5000); } } public void StopService() { start = false; }
You should not execute Thread.Sleep() on the UI thread, since that freezes the UI. There are two solutions for your problem: 1) use a separate thread, with a loop and a Thread.Sleep a while loop seems appropriate, make it while(running) where running is a bool set true and false by your buttons 2) use a Windows.Forms.Timer, start and stop it with your buttons and execute your periodic job in its tick event (without a Thread.Sleep of course). :)
Luc Pattyn
-
You should not execute Thread.Sleep() on the UI thread, since that freezes the UI. There are two solutions for your problem: 1) use a separate thread, with a loop and a Thread.Sleep a while loop seems appropriate, make it while(running) where running is a bool set true and false by your buttons 2) use a Windows.Forms.Timer, start and stop it with your buttons and execute your periodic job in its tick event (without a Thread.Sleep of course). :)
Luc Pattyn
-
Something along these lines maybe (code is not complete!):
using System.Windows.Forms;
class demo {
Timer timer;
int count;
Label myLabel;public demo() {
timer=new Timer();
timer.Interval=5000;
timer.Tick+=new EventHandler(demo_tick);
}btnStart_Click(...) {
timer.Start();
}btnStop_Click(...) {
timer.Stop();
}void demo_tick(...) {
// whatever needs to be done periodically, e.g.
count++;
// this code runs on the UI thread (because it is a forms timer)
// so we can access all UI controls
myLabel.Text="Tick #"+count;
}
}This is the simplest solution of the two, but it required the repetitive task to be short (since it runs on the UI thread). If there is too much work to do, you should go for a separate thread. :)
Luc Pattyn
-
Something along these lines maybe (code is not complete!):
using System.Windows.Forms;
class demo {
Timer timer;
int count;
Label myLabel;public demo() {
timer=new Timer();
timer.Interval=5000;
timer.Tick+=new EventHandler(demo_tick);
}btnStart_Click(...) {
timer.Start();
}btnStop_Click(...) {
timer.Stop();
}void demo_tick(...) {
// whatever needs to be done periodically, e.g.
count++;
// this code runs on the UI thread (because it is a forms timer)
// so we can access all UI controls
myLabel.Text="Tick #"+count;
}
}This is the simplest solution of the two, but it required the repetitive task to be short (since it runs on the UI thread). If there is too much work to do, you should go for a separate thread. :)
Luc Pattyn