How to exit without a return value method of infinite loop
CodePudding user response:
Restart the computer to exit
CodePudding user response:
break; End of cycle, the continue; Skip the circulation, the next cycle, I don't know I understand not
CodePudding user response:
Then the bread a layer of bai, it is good to use threads directly, but in order to show the modern style of task I used a task to do demo
Private CancellationTokenSource CTS.
//buttion1, start calling
Private async void button1_Click (object sender, EventArgs e)
{
CTS=new CancellationTokenSource ();
Task. Run (()=& gt;
{
Using (CTS) Token) Register (Thread. CurrentThread. Abort))
{
Try
{
The test ();
}
The catch (ThreadAbortException exception)
{
Trace. WriteLine (" the calling thread has been terminated, internal call to suspend the infinite ");
}
}
}, CTS. Token);
}
//button2 stop calling
Private void button2_Click (object sender, EventArgs e)
{
The CTS? The Cancel ();
}
CodePudding user response:
void test ()
{
While (true)
{
Trace. WriteLine (DateTime. Now);
Thread.sleep (500);
}
}
According to your request, give a test method of infinite loop
CodePudding user response: