private void RefreshMyDataImpl(string viewName, string liveDataSource, CancellationToken cancellationToken) { Invoke(delegate { resultsTableView.DataContext = null; }, cancellationToken); var clientLock = new object(); lock (clientLock) { var client = OGContext.ViewProcessor.CreateClient(); RoutedEventHandler pausedHandler = delegate { if (!cancellationToken.IsCancellationRequested) { client.Pause(); } }; RoutedEventHandler unpausedHandler = delegate { if (!cancellationToken.IsCancellationRequested) { client.Resume(); } }; //Must do this before we can throw any exceptions ThreadPool.RegisterWaitForSingleObject(cancellationToken.WaitHandle, delegate { lock (clientLock) { client.Dispose(); pauseToggle.Checked -= pausedHandler; pauseToggle.Unchecked -= unpausedHandler; } }, null, int.MaxValue, true); //Now we can start hooking things together pauseToggle.Checked += pausedHandler; pauseToggle.Unchecked += unpausedHandler; ComputationResultsTables resultsTable = null; int count = 0; var eventViewResultListener = new EventViewResultListener(); eventViewResultListener.ViewDefinitionCompiled += delegate(object sender, ViewDefinitionCompiledArgs args) { resultsTable = new ComputationResultsTables(_remoteSecuritySource, args.CompiledViewDefinition); Invoke(delegate { resultsTableView.DataContext = resultsTable; SetStatus(string.Format("Waiting for first cycle...")); }, cancellationToken); }; eventViewResultListener.CycleCompleted += delegate(object sender, CycleCompletedArgs e) { resultsTable.Update(e); SetStatus(GetMessage(e.FullResult ?? (IViewResultModel)e.DeltaResult, ref count)); }; eventViewResultListener.ViewDefinitionCompilationFailed += delegate(object sender, ViewDefinitionCompilationFailedArgs args) { Invoke(delegate { SetStatus(string.Format("Failed to compile {0} @ {1}", args.Exception, args.ValuationTime), true); resultsTableView.DataContext = null; }, cancellationToken); }; eventViewResultListener.CycleExecutionFailed += delegate(object sender, CycleExecutionFailedArgs args) { Invoke(delegate { SetStatus(string.Format("Failed to execute {0} @ {1}", args.Exception, args.ExecutionOptions.ValuationTime), true); }, cancellationToken); }; eventViewResultListener.ProcessTerminated += delegate { Invoke(() => SetStatus(string.Format("Process terminated @ {0}", DateTimeOffset.Now), true), cancellationToken); }; SetStatus(string.Format("Waiting for compilation...")); client.SetResultMode(ViewResultMode.FullThenDelta); client.SetResultListener(eventViewResultListener); client.AttachToViewProcess(viewName, ExecutionOptions.GetRealTime(liveDataSource)); } }