예제 #1
0
 public void Run()
 {
     int timeout = 1000;
     StayAwake sa = new StayAwake();
     Thread monitorThread = new Thread(new ThreadStart(
         () =>
         {
             // create actual work thread.
             Thread actualWorkThread = new Thread(new ThreadStart(sa.ThreadMethod));
             actualWorkThread.Name = "actual worker";
             actualWorkThread.Start();
             try
             {
                 Console.WriteLine("{0} start to sleep. {1}", Thread.CurrentThread.Name, DateTime.Now);
                 Thread.Sleep(timeout); // monitor thread sleep 10s.
                 Console.WriteLine("{0} wake up, and will interrupt worker. {1}",
                     Thread.CurrentThread.Name, DateTime.Now);
                 actualWorkThread.Interrupt();
             }
             catch (ThreadInterruptedException e)
             {
                 Console.WriteLine(">>> {0} is interrupted.", Thread.CurrentThread.Name);
             }
         }));
     monitorThread.Name = "Monitor";
     monitorThread.Start();
     sa.SleepSwitch = true;
 }
예제 #2
0
        public void Run()
        {
            StayAwake sa = new StayAwake();
            Thread newThread = new Thread(new ThreadStart(sa.ThreadMethod));
            newThread.Name = "newThread";
            newThread.Start();
            Thread.Sleep(1000);
            newThread.Interrupt();
            Console.WriteLine("Main thread call interrupt.");

            sa.SleepSwitch = true;
            newThread.Join();
        }