-
November 1st, 2005, 08:54 PM
#1
how to break from from an infinite loop based on a click event
Hi,
I am writting a applicaton in C#.net and very much new to this.
I am performing some operation infinitely when i press button A, but I want to break this infinite loop when I press button B ....I want to know which event i should use for this purpose. it would be great if someone can help me out on this.
Thanks,
M.
-
November 2nd, 2005, 02:51 AM
#2
Re: how to break from from an infinite loop based on a click event
I think you will need a thread for that.
Here is an example:
Code:
private void button1_Click(object sender, System.EventArgs e)
{
_t = new Thread(new ThreadStart(DoSomething));
_t.Start();
}
private Thread _t;
private void DoSomething()
{
try
{
while(true)
{
Thread.Sleep(0);
}
}
catch (ThreadAbortException ex)
{
MessageBox.Show("Loop break");
return;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
_t.Abort();
}
-
November 2nd, 2005, 10:27 AM
#3
Re: how to break from from an infinite loop based on a click event
You should never kill a thread using Abort unless there's a very good reason to do so. You should have an event to signal that the thread should exit e.g.
Code:
private void button1_Click(object sender, System.EventArgs e)
{
_t = new Thread(new ThreadStart(DoSomething));
_t.Start();
}
private Thread _t;
private ManualResetEvent _eventStop = new ManualResetEvent(false);
private void DoSomething()
{
while (!_eventStop.WaitOne(1, false))
{
// put your calculation code in here
}
}
private void button2_Click(object sender, System.EventArgs e)
{
_eventStop.Set();
}
Darwen.
-
November 2nd, 2005, 10:45 AM
#4
Re: how to break from from an infinite loop based on a click event
Thanks a bunch....it works.
M.
-
November 2nd, 2005, 03:47 PM
#5
Re: how to break from from an infinite loop based on a click event
Thank you Darwen, I did not know that.
Can you please explain why calling Abort is not good?
-
November 2nd, 2005, 04:09 PM
#6
Re: how to break from from an infinite loop based on a click event
Abort is used to terminate threads abnormally. The main reason for not using Abort is because there's no way of knowing at which line of code the thread will exit : so you don't know in this case whether the calculation has finished or not. It's a brute force method which could potentially put the system into an unstable state if something else relies on the result of the thread code.
Darwen.
-
November 30th, 2005, 10:50 AM
#7
Re: how to break from from an infinite loop based on a click event
So, after pressing button2 the threadstate is 'Stopped'.
Is there a way to restart/continue it?
So one can use it as a 'pause' button?
-
November 30th, 2005, 02:45 PM
#8
Re: how to break from from an infinite loop based on a click event
Darwen gave this code:
Code:
private void DoSomething()
{
while (!_eventStop.WaitOne(1, false))
{
// put your calculation code in here
}
}
You will need to save the state of the operation (in members) when the thread is stopped (after the loop), and continue the operation where it left off by reading the members (before the loop).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|