private void OnTimer(object state) { int threadId = Thread.CurrentThread.ManagedThreadId; if (_stateProcessing) { //Tools.WriteToFile(Tools.errorFile, "OnTimer() - Still Processing, Thread Id: {0}.", threadId ); return; } lock (_syncLock) { if (!_stateProcessing) { _stateProcessing = true; RecurringTaskHandler recurringTaskHandler = new RecurringTaskHandler(OnRecurringTask); IAsyncResult asyncResult = recurringTaskHandler.BeginInvoke(null, null); int timeOut = this.Timeout * ONE_SECOND; if (asyncResult.AsyncWaitHandle.WaitOne(timeOut, false)) { this.LastUpdated = DateTime.Now; object result = recurringTaskHandler.EndInvoke(asyncResult); RecurringTaskCompletedHandler recurringTaskCompletedHandler = new RecurringTaskCompletedHandler(OnRecurringTaskCompleted); recurringTaskCompletedHandler.BeginInvoke(result, this.LastUpdated.Value, null, null); } _stateProcessing = false; } else { return; } } }
private void OnTimer(object state) { TraceMessage(String.Format("OnTimer() - Thread Id: {0}.", Thread.CurrentThread.ManagedThreadId)); if (_state == TaskState.Processing) { TraceMessage(String.Format("OnTimer() - Still Processing, Thread Id: {0}.", Thread.CurrentThread.ManagedThreadId)); return; } lock (_syncLock) { if (_state != TaskState.Processing) { TaskState oldState = _state; _state = TaskState.Processing; RecurringTaskHandler recurringTaskHandler = new RecurringTaskHandler(OnRecurringTask); object result; IAsyncResult asyncResult = recurringTaskHandler.BeginInvoke(out result, null, null); int timeOut = this.Timeout * ONE_SECOND; // Wait until timout expires, or operation completes. if (asyncResult.AsyncWaitHandle.WaitOne(timeOut, false)) { bool success = recurringTaskHandler.EndInvoke(out result, asyncResult); this.LastUpdated = DateTime.Now; TraceMessage(String.Format("Last Updated: {0}, Thread Id: {1}", this.LastUpdated, Thread.CurrentThread.ManagedThreadId)); if (success) { OnRecurringTaskCompleted(result, this.LastUpdated.Value); } } else { TraceMessage(String.Format("RecurringTaskCompleted() - Timeout, Thread Id: {0}.", Thread.CurrentThread.ManagedThreadId)); } // Is case the Stop method was called an a last timer tick was in effect if (_state == TaskState.Processing) { _state = oldState; } } } }