一個多線程協助工作

來源:互聯網
上載者:User

 

 

  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

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.