public override async Task Dispose() { using (trace.NewFrame) { if (IsDisposed) { trace.Warning("Already disposed"); return; } if (listeningThread != null) { if (!listeningThread.IsAlive) { trace.Info("Thread is not alive."); } else { trace.Info("Thread has been created. Setting stop event and joining the thread."); stopEvt.Cancel(); listeningThread.Join(); trace.Info("Thread finished"); } } if (output != null) { output.Dispose(); } trace.Info("Calling base destructor"); await base.Dispose(); } }
public async Task <ResultCode> MoveToDateBound(DateTime d, bool reversedMode) { using (trace.NewFrame) { trace.Info("Moving to the date {0} than '{1}' by sending 'get {2} bound' request to all readers", reversedMode ? "less (or eq)" : "greater (or eq)", d, reversedMode ? "lower (rev)" : "lower"); ResultCode resultCode; if (reversedMode) { currentDate = MessageTimestamp.MinValue; } else { currentDate = MessageTimestamp.MaxValue; } Task <DateBoundPositionResponseData> getBoundsTask = null; for (int iteration = 0; ; ++iteration) { trace.Info("it's iteration {0} of trying to send the 'get date bound' request to reader", iteration); var modelThreadCall = invoke.Invoke(() => { // This code must be executing in the model thread using (trace.NewFrame) { if (source.IsDisposed) { trace.Warning("reader is disposed"); // This TimeGapsDetector is probably disposed too or will be soon. // Returning null will make the main algorithm wait. // During waiting it'll detect stop condition. return(null); } trace.Info("the reader is idling. Getting date bound."); return(source.GetDateBoundPosition(d, reversedMode ? ValueBound.LowerReversed : ValueBound.Lower, CancellationToken.None )); // todo: cancellation } }); trace.Info("waiting the completion of 'get date bound' request scheduler"); if (IsStopOrInvalidate(resultCode = await owner.WaitEvents(Timeout.Infinite, modelThreadCall))) { return(resultCode); } getBoundsTask = await modelThreadCall; if (getBoundsTask != null) { trace.Info("the 'get date bound' request was successfully sent to reader."); break; } trace.Info("reader is not handled. Waiting..."); if (IsStopOrInvalidate(resultCode = await owner.WaitEvents(1000, null))) { return(resultCode); } } trace.Info("waiting for the response from the reader"); if (IsStopOrInvalidate(resultCode = await owner.WaitEvents(30000, getBoundsTask))) { return(resultCode); } if (resultCode != ResultCode.UserEvent) { trace.Warning("reader didn't respond. Giving up by invalidating current progress."); return(ResultCode.Invalidate); } bool ret = HandleResponse(await getBoundsTask, reversedMode); trace.Info("returning {0}", ret); return(ret ? ResultCode.Ok : ResultCode.None); } }