/// <summary> /// Initialized and starts a new background thread that is processing the given action item. /// <para/>The process can be cancelled via the <seealso cref="CancellationTokenSource"/> /// parameter of the <seealso cref="Action"/> parameter. use this lamda expression syntax: /// <para/>... /// StartCancelableProcess( /// cts => /// { /// ... /// /// if (cts != null) /// cts.Token.ThrowIfCancellationRequested(); /// ... /// /// ProcessSomething("blah blah blah", cts); /// }, /// .. other parameters /// ); /// ... /// <para/> /// to implement the <paramref name="execFunc"/> parameter correctly. /// </summary> /// <param name="execFunc"></param> /// <param name="processFinsishedMethod"></param> /// <param name="processAlreadyRunningMsg"></param> /// <param name="processAlreadyRunningCaption"></param> /// <param name="errorMessageCaption"></param> /// <returns></returns> public bool StartCancelableProcess(Action <CancellationTokenSource> execFunc, ProcessFinishedEvent processFinsishedMethod, string processAlreadyRunningMsg) { return(this.StartProcessCancelableOrNot(execFunc, null, processFinsishedMethod, processAlreadyRunningMsg)); }
/// <summary> /// Initialized and starts a new background thread /// that is processing the given action item. /// </summary> /// <param name="execFunc"></param> /// <param name="processFinsishedMethod"></param> /// <param name="processAlreadyRunningMsg"></param> /// <param name="processAlreadyRunningCaption"></param> /// <param name="errorMessageCaption"></param> /// <returns></returns> public bool StartProcess(Action execFunc, ProcessFinishedEvent processFinsishedMethod, string processAlreadyRunningMsg) { return(this.StartProcessCancelableOrNot(null, execFunc, processFinsishedMethod, processAlreadyRunningMsg)); }
/// <summary> /// Method evaluates <paramref name="execCancelableFunc"/> or <paramref name="execFunc"/> and starts /// ans asynchronous process that can be cancelled or not, depending on whether one of the /// parameter is null or not. /// /// A valid call of this method requires that either <paramref name="execCancelableFunc"/> or /// <paramref name="execFunc"/> is null. /// </summary> /// <param name="execCancelableFunc"></param> /// <param name="execFunc"></param> /// <param name="processFinsishedMethod"></param> /// <param name="msg"></param> /// <param name="processAlreadyRunningMsg"></param> /// <param name="processAlreadyRunningCaption"></param> /// <param name="errorMessageCaption"></param> /// <returns></returns> private bool StartProcessCancelableOrNot(Action <CancellationTokenSource> execCancelableFunc, Action execFunc, ProcessFinishedEvent processFinsishedMethod, string processAlreadyRunningMsg) { try { lock (lockObject) { if (mProcessor != null) { throw new ProcessAlreadyRunningException(mProcessAlreadyRunningMsg); //// if (MsgBox.Show( //// ////"A process is currently in progress. Would you like to cancel the current process?", //// ////"Process in progress..." //// mProcessAlreadyRunningMsg, mProcessAlreadyRunningCaption, //// MsgBoxButtons.YesNo, MsgBoxImage.Question, MsgBoxResult.No) == MsgBoxResult.Yes) //// { //// if (mProcessor != null) //// { //// mProcessor.Cancel(); //// return false; //// } //// else //// return false; //// } } mProcessor = new Processor(); mProcessor.ProcessResultEvent += ProcessorResultEvent; IsProcessing = true; mProcessFinsishedMethod = processFinsishedMethod; // Initialize this here to enable usage of the previous parameters in the previous part mProcessAlreadyRunningMsg = processAlreadyRunningMsg; if (execCancelableFunc != null) { this.IsCancelable = true; mProcessor.ExecuteAsynchronously(execCancelableFunc, true); } else { this.IsCancelable = false; mProcessor.ExecuteAsynchronously(execFunc, true); } return(true); } } catch { throw; } }
static void ProcessConnection(IAsyncResult result) { try { PipeServer.EndWaitForConnection(result); // Read the request from the client. Once the client has // written to the pipe its security token will be available. ServerCommunication serverCommunication = new ServerCommunication(PipeServer); // Verify our identity to the connected client using a // string that the client anticipates. serverCommunication.SendMessage("dataDyne Chrome Server"); string command = serverCommunication.ReadMessage(); var j = new ResponseConfirmation(command); bool waitForReponse = true; bool processResponse = true; JObject reply = null; Host.MessageReceived += (s, a) => { //Handle Response if (processResponse) { reply = a.Data; } waitForReponse = false; }; Host.SendMessage(j.GetJObject()); Stopwatch sw = new Stopwatch(); sw.Start(); while (waitForReponse) { if (sw.ElapsedMilliseconds == (10 * 1000)) { //We've timed out waitForReponse = false; processResponse = false; } } string response = ""; if (processResponse && reply != null) { //Send reply back response = reply.ToString(); } else { //Reponse failed response = "{\"text\": \"Failed to communicate with chrome extension\"}"; } PipeServer.RunAsClient(() => serverCommunication.SendMessage(response)); } catch (IOException e) { Console.WriteLine("ERROR: {0}", e.Message); } PipeServer.Close(); ProcessFinishedEvent.Set(); }