| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Text;
|
|---|
| 4 | using System.Diagnostics;
|
|---|
| 5 | using System.Threading;
|
|---|
| 6 | using System.Windows.Forms;
|
|---|
| 7 |
|
|---|
| 8 | namespace Niconiconsole
|
|---|
| 9 | {
|
|---|
| 10 | class Program
|
|---|
| 11 | {
|
|---|
| 12 | static void Main(string[] args)
|
|---|
| 13 | {
|
|---|
| 14 | MainForm mainForm = new MainForm();
|
|---|
| 15 |
|
|---|
| 16 | // cmd.exe
|
|---|
| 17 | ProcessStartInfo psInfo = new ProcessStartInfo();
|
|---|
| 18 | psInfo.FileName = Environment.GetEnvironmentVariable("ComSpec");
|
|---|
| 19 | psInfo.RedirectStandardError = true;
|
|---|
| 20 | psInfo.RedirectStandardInput = true;
|
|---|
| 21 | psInfo.RedirectStandardOutput = true;
|
|---|
| 22 | psInfo.UseShellExecute = false;
|
|---|
| 23 | psInfo.CreateNoWindow = true;
|
|---|
| 24 | psInfo.WindowStyle = ProcessWindowStyle.Normal;
|
|---|
| 25 |
|
|---|
| 26 | using (Process p = Process.Start(psInfo))
|
|---|
| 27 | {
|
|---|
| 28 | Boolean skipInputLine = false;
|
|---|
| 29 | Int32 lineCount = 0;
|
|---|
| 30 | StringBuilder sb = new StringBuilder();
|
|---|
| 31 | DataReceivedEventHandler dataRecvEventHandler = delegate(Object sender, DataReceivedEventArgs e)
|
|---|
| 32 | {
|
|---|
| 33 | if (String.IsNullOrEmpty(e.Data)) return;
|
|---|
| 34 |
|
|---|
| 35 | if (skipInputLine)
|
|---|
| 36 | {
|
|---|
| 37 | skipInputLine = false;
|
|---|
| 38 | return;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | String outputLine = e.Data;
|
|---|
| 42 | lineCount++;
|
|---|
| 43 |
|
|---|
| 44 | System.Diagnostics.Debug.WriteLine(outputLine);
|
|---|
| 45 | mainForm.Enqueue(outputLine, false);
|
|---|
| 46 | };
|
|---|
| 47 | DataReceivedEventHandler errDataRecvEventHandler = delegate(Object sender, DataReceivedEventArgs e)
|
|---|
| 48 | {
|
|---|
| 49 | if (String.IsNullOrEmpty(e.Data)) return;
|
|---|
| 50 |
|
|---|
| 51 | if (skipInputLine)
|
|---|
| 52 | {
|
|---|
| 53 | skipInputLine = false;
|
|---|
| 54 | return;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | String outputLine = e.Data;
|
|---|
| 58 | lineCount++;
|
|---|
| 59 |
|
|---|
| 60 | System.Diagnostics.Debug.WriteLine(outputLine);
|
|---|
| 61 | mainForm.Enqueue(outputLine, true);
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | p.OutputDataReceived += dataRecvEventHandler;
|
|---|
| 66 | p.ErrorDataReceived += errDataRecvEventHandler;
|
|---|
| 67 | p.BeginOutputReadLine();
|
|---|
| 68 | p.BeginErrorReadLine();
|
|---|
| 69 |
|
|---|
| 70 | //
|
|---|
| 71 | Thread inputThread = new Thread(delegate()
|
|---|
| 72 | {
|
|---|
| 73 | Console.Write("> ", Environment.CurrentDirectory);
|
|---|
| 74 | String line = "";
|
|---|
| 75 | while (!p.HasExited && (line = Console.ReadLine()) != null)
|
|---|
| 76 | {
|
|---|
| 77 | skipInputLine = true;
|
|---|
| 78 | p.StandardInput.WriteLine(line);
|
|---|
| 79 | Console.Write("> ");
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | Application.Exit();
|
|---|
| 83 | });
|
|---|
| 84 |
|
|---|
| 85 | inputThread.Start();
|
|---|
| 86 |
|
|---|
| 87 | mainForm.FormClosing += new FormClosingEventHandler(delegate(Object sender, FormClosingEventArgs e)
|
|---|
| 88 | {
|
|---|
| 89 | p.Close();
|
|---|
| 90 | inputThread.Abort();
|
|---|
| 91 | });
|
|---|
| 92 |
|
|---|
| 93 | Application.Run(mainForm);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|