1

隠しコンソール CMD を使用するプログラムを作成しています。コーデック「ffmpeg」で開始します。常に変換するとフィードバックが得られます。

コンソールから 1 秒ごとに料金が発生することを願っています。残念ながら、インターネットで入手できるすべてのコードは、変換後に返金されるという原則に基づいて機能します。


public void ExecuteCommandSync(object command)
{
     try
     {
         // create the ProcessStartInfo using "cmd" as the program to be run,
         // and "/c " as the parameters.
         // Incidentally, /c tells cmd that we want it to execute the command that follows,
         // and then exit.
    System.Diagnostics.ProcessStartInfo procStartInfo =
        new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

    // The following commands are needed to redirect the standard output.
    // This means that it will be redirected to the Process.StandardOutput StreamReader.
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    // Do not create the black window.
    procStartInfo.CreateNoWindow = true;
    // Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    // Get the output into a string
    string result = proc.StandardOutput.ReadToEnd();
    // Display the command output.
    Console.WriteLine(result);
      }
      catch (Exception objException)
      {
      // Log the exception
      }
} 
4

2 に答える 2

4

待たずに出力を取得したい場合は、非同期機能を使用してください。チェックアウトhttp://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx

イベントハンドラを設定する

proc.OutputDataReceived += (s,e) => { Console.WriteLine(e.Data);};

標準出力の非同期読み取りを開始

proc.BeginOutputReadLine();

これらの機能を使用するようにコードを修正しました。

    public static void ExecuteCommandSync(object command)
    {
        try
        {
            // create the ProcessStartInfo using "cmd" as the program to be run,
            // and "/c " as the parameters.
            // Incidentally, /c tells cmd that we want it to execute the command that follows,
            // and then exit.
            var procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);


            // The following commands are needed to redirect the standard output.
            // This means that it will be redirected to the Process.StandardOutput StreamReader.
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;
            // Now we create a process, assign its ProcessStartInfo and start it
            var proc = new System.Diagnostics.Process();
            proc.OutputDataReceived += (s,e) => { Console.WriteLine(e.Data);};
            proc.StartInfo = procStartInfo;
            proc.Start();
            proc.BeginOutputReadLine();
            proc.WaitForExit();
        }
        catch (Exception objException)
        {
            Console.WriteLine("Error: " + objException.Message);
            // Log the exception
        }
    }
于 2012-06-30T11:40:31.023 に答える
0

Backgroundworker の Backgroundworker クラスMSDN を参照することをお勧めします。

ここでは、進行状況を表示するための単純なコールバック メカニズムが用意されています。進行状況の更新を行うハンドラーを提供するだけで済みます。

MSDN から:

// This event handler updates the progress bar.
private void backgroundWorker1_ProgressChanged(object sender,
    ProgressChangedEventArgs e)
{
    this.progressBar1.Value = e.ProgressPercentage;
}

とともに

public void ReportProgress(
int percentProgress,
Object userState)

関数は、必要な状態の変更、特に必要な可能性のある動的な進行状況 (ワーカーで何が起こっているか) を返すことができます。

しかし、あなたの「リアルタイム」の部分についてはよくわかりません。

于 2012-06-30T11:17:18.397 に答える