protected override void StartSaveTask(long dbId, byte[] template, Guid guid, CancellationToken token) { // Make HTTP request HttpContent content = new ByteArrayContent(template); Task <HttpResponseMessage> saveResponse = m_Client.PostAsync(SimTemplateServerHelper.SaveTemplateUri(dbId), content); // When task is complete, raise GetCaptureComplete event // Pass the task the cancellation token so that this action may be skipped saveResponse.ContinueWith((Task <HttpResponseMessage> sTT) => { // Check for cancellation (race) if (!token.IsCancellationRequested) { // HTTP requests are not cancellable IntegrityCheck.IsFalse(sTT.IsCanceled); if (sTT.IsCompleted) { HttpResponseMessage response = sTT.Result; // Do some dealing with the response to check it was successful OnSaveTemplateComplete( new SaveTemplateEventArgs(guid, DataRequestResult.Success)); } else if (sTT.IsFaulted) { // An exception was thrown during the request. Log.Error("Save Template task failed: " + sTT.Exception.Message, sTT.Exception); OnSaveTemplateComplete( new SaveTemplateEventArgs(guid, DataRequestResult.TaskFailed)); } } }, token); }
protected override void StartCaptureTask(ScannerType scannerType, Guid guid, CancellationToken token) { Log.DebugFormat("Starting task with Guid={0}", guid); // Make HTTP request Task <string> responseText = m_Client.GetStringAsync( SimTemplateServerHelper.GetCaptureRequestUri(scannerType)); // When task is complete, raise GetCaptureComplete event // Pass the task the cancellation token so that this action may be skipped responseText.ContinueWith((Task <string> gCT) => { // Check for cancellation (race) if (!token.IsCancellationRequested) { // HTTP requests are not cancellable IntegrityCheck.IsFalse(gCT.IsCanceled); if (gCT.IsCompleted) { CaptureInfo capture; try { capture = ProcessResponse(gCT.Result); OnGetCaptureComplete( new GetCaptureCompleteEventArgs(capture, guid, DataRequestResult.Success)); } catch (SimTemplateException ex) { Log.Error("Failed to process xml response:", ex); OnGetCaptureComplete( new GetCaptureCompleteEventArgs(null, guid, DataRequestResult.Failed)); } } else if (gCT.IsFaulted) { // An exception was thrown during the request. Log.Error("GetCapture task failed: " + gCT.Exception.Message, gCT.Exception); OnGetCaptureComplete( new GetCaptureCompleteEventArgs(null, guid, DataRequestResult.TaskFailed)); } } }, token); }
private T ToState(Type stateType) { IntegrityCheck.IsTrue( typeof(T).IsAssignableFrom(stateType), "Supplied type doesn't derive from {0}", typeof(T).Name); IntegrityCheck.IsFalse( stateType.IsAbstract, "Cannot transition to abstract state {0}", stateType.Name); T state; bool isFound = m_States.TryGetValue(stateType, out state); IntegrityCheck.IsTrue(isFound, "State {0} not found in m_States", stateType.Name); IntegrityCheck.IsNotNull( state, "State {0} found in m_States, but value was null", stateType.Name); return(state); }