茶不二的小说哪个好看:线程的挂起与继续

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 04:18:22
我想在一个鼠标进入一个Form时挂起一个正在运行的线程,当鼠标离开时继续这个线程, 
我想应该是要用到   Suspend与Resume。 

我的程序现在是:在鼠标进入时Suspend是起作用了,线程的确停下了。但鼠标离开时却没有作用,而且好象整个程序都停下了。 

请问我的做法有什么地方不对吗?应该怎么做?
下面例子中对线程t,Suspend和Resume都好用。 

private   static   int   count   =   0; 
private   bool   flag   =   true; 
private   Thread   t   =   new   Thread(new   ThreadStart(ThreadProc)); 

private   void   Form1_Load(object   sender,   System.EventArgs   e) 

t.Start(); 


public   static   void   ThreadProc()   

while   (true) 

count   ++; 
Console.WriteLine( "count:   {0} ",   count); 
Thread.Sleep(1000); 



private   void   label1_Click(object   sender,   System.EventArgs   e) 

try 

if   (flag)   t.Suspend(); 
else   t.Resume(); 
flag   =   !flag; 

catch   (ThreadStateException   ex) 

Console.WriteLine( "ThreadStateException:{0} ",   ex.Message); 

catch(Exception   ex) 

Console.WriteLine( "Exception:{0} ",   ex.Message); 
}
}