Seasons.NET

ちょっとした技術ブログです

ProcessでCygwinを操作したい!!(1)

id:Ozyさんと協力して調べている問題。
とりあえずメモということで。

Threadの処理が甘いのは、仮組みということで
勘弁してもらって、これであれば、/home/XXX( ユーザーフォルダ )
にlogfileが作成され、出力結果が保存される。
どうも cmd.exeからたたいただけでは無理っぽいようだ。

とりあえずログインして、makeをたたいて、出力を保存してみる方法

あとは、このファイルをリードしてテキストボックスに出力すればいいんかなぁ?
ちなみにこのコードは、gvimのTOhtmlコマンドで出力したもの。

 1  using System;
 2  using System.Collections.Generic;
 3  using System.ComponentModel;
 4  using System.Data;
 5  using System.Drawing;
 6  using System.Text;
 7  using System.Windows.Forms;
 8  using System.Diagnostics;
 9  using System.IO;
10  using System.Threading;
11
12  namespace PipeSample1
13  {
14      public partial class Form1 : Form
15      {
16          private Thread prThread_ = null;
17          private Thread outThread_ = null;
18          private bool processStart_ = false;
19          private StreamReader processReader_ = null;
20          private Process process_ = null;
21
22          public Form1()
23          {
24              InitializeComponent();
25              processStart_ = false;
26              prThread_ = new Thread(new ThreadStart(processStartThread));
27          }
28
29          private void processOutThread()
30          {
31              int i = 0;
32              while ( true )
33              {
34                  if (processStart_ && process_ != null)
35                  {
36                      if (processReader_ != null)
37                      {
38                          string line = processReader_.ReadLine();
39                          if (line == null)
40                          {
41                              process_.Close();
42                              processReader_.Close();
43                              processStart_ = false;
44                              MessageBox.Show("終了");
45                              break;
46                          }
47                          textBox1.AppendText( (line+"\n") );
48                      }
49                  }
50                  Thread.Sleep(100);
51              }
52          }
53
54          private void processStartThread()
55          {
56              if (processStart_ == false)
57              {
58                  process_ = new Process();
59                  ProcessStartInfo pifo = new ProcessStartInfo("C:\\cygwin\\bin\\bash.exe");
60
61                  pifo.UseShellExecute = false;
62                  pifo.RedirectStandardOutput = true;
63                  pifo.RedirectStandardInput = true;
64                  // ログイン用
65                  pifo.Arguments = "--login -i";
66                  pifo.WorkingDirectory = Directory.GetCurrentDirectory();
67                  // ウィンドウを表示しない
68                  pifo.CreateNoWindow = true;
69                  pifo.WindowStyle = ProcessWindowStyle.Hidden;
70                  process_.StartInfo = pifo;
71                  // プロセススタート
72                  process_.Start();
73                  processReader_ = process_.StandardOutput;
74                  processStart_ = true;
75                  outThread_.Start();
76
77                  // 出力をいったんファイルに書き出し -> logfile
78                  //
79                  // logfileをさらにテキストボックスに出力すればOK?
80                  //
81                  process_.StandardInput.WriteLine("make.exe &> logfile");
82                  // cygwinをログアウト
83                  process_.StandardInput.WriteLine("exit");
84              }
85          }
86
87          private void button1_Click(object sender, EventArgs e)
88          {
89              if (processStart_ == false)
90              {
91                  textBox1.Clear();
92                  if( outThread_ != null ) outThread_.Abort();
93                  outThread_ = new Thread(new ThreadStart(processOutThread));
94                  prThread_.Start();
95              }
96          }
97
98      }
99  }