Home > Net >  Void method can infinite loop exit
Void method can infinite loop exit

Time:01-22

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:

reference wanghui0380 reply: 3/f
outside 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 ();
}

Small white said I didn't understand QWQ under this code you typed and then click the button or no response said

CodePudding user response:

 class loop 
{
Public void shuchu ()
{
String STR=the Convert. ToString (Console. ReadLine ());
Int len=STR. Length;
Console. WriteLine (" length for {0} ", len);
Shuchu ();
return 0;
}
}
The class Xunhuan
{
The static void Main (string [] args)
{
Loop p=new loop ();
P. huchu ();
Console.ReadLine();
}
}


This is my own infinite loop, returns a value method can quit, want to know how to refund void method

CodePudding user response:

Small white said I didn't understand QWQ under this code you typed and then click the button or no response said

Have no reaction

The
reference
void test ()
{
While (true)
{
Trace. WriteLine (DateTime. Now);
Thread.sleep (500);
}
}


The Trace class is giving the ans tracking debug, not the program output interface, he displayed below when you are debugging to vs the output window and output of information, use this just said here in the test run, every 500 ms to the output window and output a DateTime. Now show that the test method of living,

Button2 stopped, he won't output

CodePudding user response:

Loop to write a trigger condition. The condition to throw an exception or break out of circulation

CodePudding user response:

Declare a public variable
 
Private static bool flag=true;
Private void start_Click (object sender, EventArgs e) {
While (flag)
{

}
}
Private void stop_Click (object sender, EventArgs e) {
Flag=false;
}

CodePudding user response:

Break only out of the loop inside and can't quit method,
  •  Tags:  
  • C#
  • Related