现在的位置: 首页 > 综合 > 正文

一个多线程协助工作

2011年03月18日 ⁄ 综合 ⁄ 共 2865字 ⁄ 字号 评论关闭

 

 

  public class Worker
    {
        
// This method will be called when the thread is started.
        public void DoWork()
        {  
            Console.WriteLine(
"主线程成功启动!");
            
while (!_shouldStop)
            {
               
                Console.WriteLine( DateTime.Now.ToString(
"yyyyMMddHHmmss")+":主进程正在工作!" );
                guard.guardId 
= true;
                GetNewEmails();
                System.Threading.Thread.Sleep(
1000 * refreshinterval);

            }
            Console.WriteLine("主线程关闭!");
        }
        
public void RequestStop()
        {
            _shouldStop 
= true;
        }
        
public void RequestOpen()
        {
            _shouldStop 
= false;
        }
        
public bool shouldStop
        {
            
get { return _shouldStop; }
            
set { _shouldStop = value; }
        }
    
        
private bool _shouldStop = false;
}

 

 

 

guard.cs

 

 

 

 

 

    static class Program
    {  

      
        /// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>
        static public  Worker workerObject = new Worker();
        
static public  guard guardobject = new guard();
        
static public  Thread workerThread = null;
        
static public  Thread  guardThread=null;
        
static void Main()
        {
           workerThread 
= new Thread(workerObject.DoWork);
           guardThread 
= new Thread(guard.guardThread);
           workerThread.Start();
           guardThread.Start();
        }
    
    }

 

抱歉!评论已关闭.