public void WriteThread() { try { while ((std_output = WProcessObj.StandardOutput.ReadLine()) != null) { WTabRT.WriteLine(std_output); } } catch (Exception ed) { Console.WriteLine($"Error Occured in writing to Tab RT." + ed.Message); } }
public async Task RunProcessAsync(string ansicon_cmd, string plink_cmd) { // Execute plink.exe // Exit on error and close the tab // Else stay on the tab, wait for the user input // and return output. // Create a tab VCustomTab myTabPage = new VCustomTab(""); try { using (Process myProcess = new Process()) { String macname = macTxtBox.Text; String usrname = usrTxtBox.Text; String pwd = pwdTxtBox.Text; //String args = plink_cmd + " -l " + usrname + " -pw " + pwd + " " + macname; String args = $"{plink_cmd} -l {usrname} -pw {pwd} {macname}"; ProcessStartInfo startInfo = new ProcessStartInfo(ansicon_cmd, args); startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.RedirectStandardInput = true; // plink.exe is the cmd tool for putty. myProcess.StartInfo = startInfo; //myProcess.OutputDataReceived += CaptureOutput; //myProcess.ErrorDataReceived += CaptureError; // This code assumes the process you are starting will terminate itself. // Given that is is started without a window so you cannot terminate it // on the desktop, it must terminate itself or you can do it programmatically // from this application using the Kill method. myProcess.OutputDataReceived += ProcessOutputHandler; String outstr = "vj"; myProcess.Start(); tabProc = new TabProcess(myProcess.StandardInput, myProcess.StandardOutput); StreamWriter process_std_input_stream = myProcess.StandardInput; String tempStr = ""; //outstr = await ReadFromProcessAsync(myProcess.StandardOutput); outstr = await tabProc.ReadFromProcessAsync(); tempStr += outstr; //outstr = await ReadFromProcessAsync(myProcess.StandardOutput); outstr = await tabProc.ReadFromProcessAsync(); tempStr += outstr; string nextcmd = "\n"; // Enter after success //await WriteToProcessAsync(myProcess.StandardInput, nextcmd); await tabProc.WriteToProcessAsync(nextcmd); ANSIEscapeSeq ansi_seq = new ANSIEscapeSeq(); // TODO: The end with character may differ : Taken Care, ANSIEscapeSeq class while (outstr != null && !outstr.Trim().EndsWith("$")) { tempStr += outstr + "\n"; //outstr = await ReadFromProcessAsync(myProcess.StandardOutput); outstr = await tabProc.ReadFromProcessAsync(); /* * byte[] escTitleBytesRec = Encoding.ASCII.GetBytes(outstr); * foreach (byte b in escTitleBytesRec) * Console.Write((char)b + "( " + b + ") "); */ //myTabPage.WriteLine(outstr + "\n"); } // Get the command prompt and title now. var out_tup = ansi_seq.getTitleAndCommandPrompt(outstr); myTabPage.setTitle(out_tup.Item1); myTabPage.setPrompt(out_tup.Item2); myTabPage.setProcessTab(tabProc); tabControl1.TabPages.Add(myTabPage); myTabPage.WriteLine(tempStr); myTabPage.WriteLine(out_tup.Item2); // Loop until the command is logout or exit or on error. //await ReadFromMemStreamAsync(); //outstr = await ReadFromProcessAsync(myProcess.StandardOutput); Console.WriteLine($"Form1: The output we got is: {outstr}"); /* * try * { * Thread.Sleep(10000); * //nextcmd = "logout"; * nextcmd = ""; * await WriteToProcessAsync(myProcess.StandardInput, nextcmd); * } * catch (Exception eex) * { * Console.WriteLine($"An error occurred {eex.Message}"); // The $ is the fstrings in python. * } * * if (myProcess.HasExited) * { * myProcess.Close(); * myProcess.Dispose(); * } * else * { * myProcess.Kill(); * } */ } } catch (Exception ex) { Console.WriteLine(ex.Message); } }