Example #1
0
        } // OnFeedbackRichTextBoxTextChanged

        /// <summary>
        ///     Responds to a returning from a WriteAsync pipe operation by giving
        ///     feedback to User and waiting server's feedback.
        /// </summary>
        /// <param name="sender"> The async pipe task </param>
        /// <param name="operation"> Specifies the command string </param>
        /// <param name="form"> The form object </param>
        private void OnReturningFromAsyncPipeOperation(object sender, string operation, MainForm form)
        {
            try
            {
                Task task = sender as Task;

                WriteVerbose($"Feedback UI to {{{operation}}} ... ", true);

                // Some milliseconds to complete and we are good
                Task.Delay(1000);
                // Give feedback for async task operation
                GiveFeedbackToUiInput(task, operation, true, form);

                WriteVerbose("OK", false, "White", form);

                // Wait for server's feedback
                int    size   = 1024;
                var    buffer = new byte[size];
                string data   = System.String.Empty;

                if (null == Pipe)
                {
                    return;
                }

                WriteVerbose
                (
                    "Waiting for response from server ... ", true,
                    "White", form
                );

                // Check connection
                if (!Pipe.IsConnected)
                {
                    try
                    {
                        Pipe.Connect(1000);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine
                        (
                            "OnReturningFromAsyncPipeOperation#Connect: " + ex.Message
                        );
                    }
                }

                if (Pipe.IsConnected)
                {
                    // Clean buffer
                    for (int i = 0; i < buffer.Count(); i++)
                    {
                        buffer[i] = Encoding.GetBytes("0")[0];
                    }
                    // Read data, if any
                    // Data format: status#command#category#source#execution_date
                    try
                    {
                        // Some milliseconds for the server to respond and we are good
                        Task.Delay(ServerResponseTimeout);
                        var T = Pipe.ReadAsync
                                (
                            buffer, 0, 1, PipeReadCancellationToken
                                );
                        if (T.IsCompleted)
                        {
                            int N = T.Result;
                            if (N > 0)
                            {
                                data += Encoding.GetString(buffer);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine
                        (
                            "OnReturningFromAsyncPipeOperation#ReadAsync: " +
                            ex.Message
                        );
                    }

                    // Write feedback to user
                    if (!System.String.IsNullOrEmpty(data))
                    {
                        WriteVerbose("OK");

                        string message =
                            InterpreteFeedbackFromPipeServerStream(data);

                        // Fire feedback event
                        GiveFeedbackToUiInput(task, message, false, form);

                        data = System.String.Empty;
                    }
                    else
                    {
                        WriteVerbose("No Response");
                    }
                }
                else
                {
                    WriteVerbose("Disconnected");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine
                (
                    "OnReturningFromAsyncPipeOperation: " + ex.Message
                );
            }
        } // OnReturningFromAsyncPipeOperation