操作方法
sleep(0)无用,产生死循环耗死了cpu,所以一定要有个时间间隔。 代码如下: amespace whiletest { public partial class Form1 : Form { int time = 0; Thread s; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { time =Convert.ToInt32( this.textBox1.Text); s = new Thread(doit); s.Start(); } private void doit() { while (true) { System.Threading.Thread.Sleep(time); Console.WriteLine(System.DateTime.Now); }; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { s.Abort(); } private void button2_Click(object sender, EventArgs e) { s.Abort(); } } } time=0 cpu得不到休息,专用率100%