一个多线程协助工作

 

 

   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 ;
}

 

 

 

ContractedBlock.gif ExpandedBlockStart.gif guard.cs

  
class guard
    {

       
static  public bool guardId = false;
       
static public int guardinterval = Convert.ToInt32(ConfigurationManager.AppSettings["guardinterval"]);
       
static  public void guardThread()
        {
            
while (true)
            {
                System.Threading.Thread.Sleep(
1000 * guardinterval);
               
                
if (!guardId)
                {
                    Console.WriteLine(
"主线程异常!");

                    
try
                    {
                       
// Program.workerThread.Abort();
                        Program.workerObject.RequestStop();
                        Program.workerThread.Join();
                        Thread.Sleep(
6000);
                        Program.workerThread 
= null;
                        Console.WriteLine(
"主线程赋值为空!");
                        Program.workerThread 
= new Thread(Program.workerObject.DoWork);
                        Console.WriteLine(
"主线程重新创建!");
                        Program.workerThread.Start();
                        Program.workerObject.RequestOpen();
                       
                    }
                    
catch (Exception e)
                    {
                        Console.WriteLine(
"守护进程:" + e.Message);
                    }
               
                    
                }
                
else
                {
                    Console.WriteLine(
"主线程正常!");
                }

                guardId 
= false;
            }

        }
    }

 

 

 

 

 

     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();
        }
    
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值