コード例 #1
0
 void _windowEvents_WindowActivated(EnvDTE.Window GotFocus, EnvDTE.Window LostFocus)
 {
     if (IsInDebugLayout())
     {
         _scheduler.Stop();
     }
     else
     {
         _scheduler.Start();
     }
 }
        public void TestStartAndStop()
        {
            int NUM_TASKS = 10;
            int STOP_NUM  = 3;
            int START_NUM = 6;
            int TIMEOUT   = 60000;

            var scheduler          = new LimitedConcurrencyLevelTaskScheduler(4);
            var factory            = new TaskFactory(scheduler);
            var rng                = new Random();
            int currentlyExecuting = 0;

            Action <int> testAction = (int i) => {
                int value = Interlocked.Increment(ref currentlyExecuting);
                Thread.Sleep(i);
                Interlocked.Decrement(ref currentlyExecuting);
            };

            Task[] tasks = new Task[NUM_TASKS];
            for (int i = 0; i < NUM_TASKS; i++)
            {
                tasks[i] = factory.StartNew(() => testAction(rng.Next(100, 500)));
                if (i == STOP_NUM)
                {
                    scheduler.Stop();
                    for (int j = 0; j < NUM_TASKS; j++)
                    {
                        if (0 == currentlyExecuting)
                        {
                            Console.WriteLine("Slept for {0} ms", j * 500);
                            break;
                        }
                        Thread.Sleep(500);
                    }
                }
                if (i >= STOP_NUM && i <= START_NUM)
                {
                    Assert.AreEqual(0, currentlyExecuting);
                }
                if (i == START_NUM)
                {
                    scheduler.Start();
                }
            }
            Task.WaitAll(tasks);
            Assert.AreEqual(0, currentlyExecuting);
        }