A TaskRunner that dedicates a single thread to running a single Task.
상속: TaskRunner
        public void TestCompositeTaskRunner()
        {
            int attempts = 0;

            CompositeTaskRunner runner = new CompositeTaskRunner();

            CountingTask task1 = new CountingTask("task1", 100);
            CountingTask task2 = new CountingTask("task2", 200);

            runner.AddTask( task1 );
            runner.AddTask( task2 );

            runner.Wakeup();

            while( attempts++ != 10 )
            {
                Thread.Sleep( 1000 );

                if(task1.Count == 100 && task2.Count == 200)
                {
                    break;
                }
            }

            Assert.IsTrue(task1.Count == 100);
            Assert.IsTrue(task2.Count == 200);

            runner.RemoveTask(task1);
            runner.RemoveTask(task2);
        }
예제 #2
0
		private void StopMonitorThreads()
		{
			lock(monitor)
			{
				if(monitorStarted.CompareAndSet(true, false))
				{
					AutoResetEvent shutdownEvent = new AutoResetEvent(false);

					if(null != connectionCheckTimer)
					{
						// Attempt to wait for the Timer to shutdown, but don't wait
						// forever, if they don't shutdown after two seconds, just quit.
						this.connectionCheckTimer.Dispose(shutdownEvent);
						if(!shutdownEvent.WaitOne(TimeSpan.FromMilliseconds(3000), false))
						{
							Tracer.WarnFormat("InactivityMonitor[{0}]: Timer Task didn't shutdown properly.", instanceId);
						}

						this.connectionCheckTimer = null;
					}

					if(null != this.asyncTasks)
					{
						this.asyncTasks.RemoveTask(this.asyncWriteTask);
						this.asyncTasks.RemoveTask(this.asyncErrorTask);

						this.asyncTasks.Shutdown();
						this.asyncTasks = null;
					}

					this.asyncWriteTask = null;
					this.asyncErrorTask = null;
				}
			}

			Tracer.DebugFormat("InactivityMonitor[{0}]: Stopped Monitor Threads.", instanceId);
		}
예제 #3
0
		private void StartMonitorThreads()
		{
			lock(monitor)
			{
				if(this.IsDisposed || this.disposing)
				{
					return;
				}

				if(monitorStarted.Value)
				{
					return;
				}

				if(localWireFormatInfo == null)
				{
					return;
				}

				if(remoteWireFormatInfo == null)
				{
					return;
				}

				readCheckTime =
					Math.Min(
						localWireFormatInfo.MaxInactivityDuration,
						remoteWireFormatInfo.MaxInactivityDuration);
				initialDelayTime = remoteWireFormatInfo.MaxInactivityDurationInitialDelay > 0 ?
					Math.Min(localWireFormatInfo.MaxInactivityDurationInitialDelay,
						     remoteWireFormatInfo.MaxInactivityDurationInitialDelay) :
                    localWireFormatInfo.MaxInactivityDurationInitialDelay;

				if(readCheckTime > 0)
				{
					Tracer.DebugFormat("InactivityMonitor[{0}]: Read Check time interval: {1}",
								   instanceId, readCheckTime);
					Tracer.DebugFormat("InactivityMonitor[{0}]: Initial Delay time interval: {1}",
									   instanceId, initialDelayTime);

					monitorStarted.Value = true;
					this.asyncTasks = new CompositeTaskRunner("InactivityMonitor[" + instanceId + "].Runner");

					this.asyncErrorTask = new AsyncSignalReadErrorkTask(this, next.RemoteAddress);
					this.asyncWriteTask = new AsyncWriteTask(this);

					this.asyncTasks.AddTask(this.asyncErrorTask);
					this.asyncTasks.AddTask(this.asyncWriteTask);

					writeCheckTime = readCheckTime > 3 ? readCheckTime / 3 : readCheckTime;

					Tracer.DebugFormat("InactivityMonitor[{0}]: Write Check time interval: {1}",
									   instanceId, writeCheckTime);

					this.connectionCheckTimer = new Timer(
						new TimerCallback(CheckConnection),
						null,
						initialDelayTime,
						writeCheckTime
						);
				}
			}
		}