2016-07-28 10 views
0

コマンドを実行してコマンドを実行して出力を取得するコードが少しありますが、常に2回実行され、出力が欠落することがあります。ここでC#ProcessStartInfoは常に重複したプロセスを実行します

enter image description here

は私のコードですが、私は何度もチェックを再が、何の原因を把握することはできません。

using System; 
using System.Diagnostics; 
using System.IO; 
using System.Security.Cryptography; 
using System.Security.Permissions; 
using System.Text; 
using System.Threading; 


namespace CommandHandler 
{ 
    class Program 
    { 
     public static string change_file = AppDomain.CurrentDomain.BaseDirectory + @"change\change.txt"; 
     public static void Main() 
     { 
      //Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight); 
      Console.BackgroundColor = ConsoleColor.Black; 
      Console.ForegroundColor = ConsoleColor.Green; 

      //ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c php d:/test.php") 
      ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c D:\\php64\\php D:\\xampp\\htdocs\\xxx\\bin\\listen.php") 
      { 
       WindowStyle = ProcessWindowStyle.Hidden, 
       UseShellExecute = false, 
       RedirectStandardOutput = true, 
       CreateNoWindow = true 
      }; 

      Process process = Process.Start(startInfo); 
      //process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived); 
      process.OutputDataReceived += new DataReceivedEventHandler((sender, e) => 
      { 
       // Prepend line numbers to each line of the output. 
       if (!String.IsNullOrEmpty(e.Data)) 
       { 
        String value = e.Data.ToLower(); 
        Console.WriteLine(e.Data); 
        if (value.Contains("php fatal error:")) 
        { 
         string hash = md5(DateTime.Now.ToString()); 
         System.IO.File.WriteAllText(change_file, hash); 
        } 
       } 
      }); 
      process.BeginOutputReadLine(); 
      process.Start(); 
      process.WaitForExit(); 
      Console.ReadKey(); 
     } 

     public static byte[] encryptData(string data) 
     { 
      System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider(); 
      byte[] hashedBytes; 
      System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
      hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data)); 
      return hashedBytes; 
     } 

     public static string md5(string data) 
     { 
      return BitConverter.ToString(encryptData(data)).Replace("-", "").ToLower(); 
     } 
    } 
} 

+1

多分それはですか)? – OrMiz

+1

正直なところ、なぜあなたが下票を持っているのか分かりません。どんな正当な理由であれ、そうではありません。 –

+0

@roryap私は投票または投票を気にしません。私はちょうど私の問題を解決したいと思った、私は彼らのCVにStackoverflowで自分のスコアを取る多くの人々を知っていると私の意見では、それは本当に時間の無駄です。しかし、あなたが投票をクリックすると、私は本当に楽しんでいます。 – vietnguyen09

答えて

5

Process.Start()を2度呼び出しているためです。ここでは、インスタンスprocess作成するとき:あなたのコンストラクタの底部近く、

ここ
Process process = Process.Start(startInfo); 

と再びを:あなたはprocess.Start(2回呼び出すので

process.Start(); 
関連する問題