Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
button1()
{
fun()
}

button2()
{

}

fun()
{
while()
print(something);
}
Posted
Updated 11-Sep-12 3:04am
v2
Comments
[no name] 11-Sep-12 9:03am    
Have button 2 make the while condition false.
Member 9092048 11-Sep-12 9:04am    
can u pls show how to do
Kenneth Haugland 11-Sep-12 9:06am    
make a global bool variable and set this true or false by the click of a button.

Here is a sample having the loop in a thread:
C#
private void button1_Click(object sender, EventArgs e)
{
   System.Threading.Thread thr = new Thread(ThreadProc);
   thr.Start();
}

private void button2_Click(object sender, EventArgs e)
{
   bCancel = true;
}

bool bCancel = false;
private void ThreadProc(object o)
{
   while (!bCancel)
   {
      //Do some work
      Thread.Sleep(1);
   }
   MessageBox.Show("Loop finished");
}
 
Share this answer
 
Hi,

Executing infinite while is not preferable. I personally suggest you to use BackgroundWorker to do your long run process. BackgroundWorker support more then what you want.

Here are some good link to do that.

MSDN : BackgroundWorker[^]

http://www.dotnetperls.com/backgroundworker[^]

BackgroundWorker Threads and Supporting Cancel[^]

Hope this works for you,
Thanks
-Amit Gajjar
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900