0

アプリケーション内からいくつかの外部アプリケーションを実行しようとしています。LongtimeRun.exe というアプリケーションを 10 回実行したいとします。このアプリケーションを実行するたびに、完了するまでに約 30 秒かかります (合計時間は 300 秒または 5 分です!)。また、ユーザーに進行状況を示したい (たとえば、アプリケーションの実行回数など)。

バッチ ファイルを作成して、そこで LongTimeRun.exe を 10 回実行することはできますが、その後、進捗レポートを表示できません。

私は動作するこのコードを持っています:

using System.Diagnostics;
using System.IO;

public class CommandProcessor
{
        private readonly string binDirectory;
    private readonly string workingDirectory;
    public CommandProcessor(string workingDirectory, string binFolderName)
    {
        binDirectory = Path.Combine(FileSystem.ApplicationDirectory, binFolderName);
        this.workingDirectory = workingDirectory;
    }
    public int RunCommand(string command, string argbase, params string[] args)
    {
        var commandPath = Path.Combine(binDirectory, command);
        var formattedArgumets = string.Format(argbase, args);

        var myProcess = new Process();
        myProcess.EnableRaisingEvents = false;
        myProcess.StartInfo.FileName = commandPath;
        myProcess.StartInfo.Arguments = formattedArgumets;
        myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        myProcess.StartInfo.WorkingDirectory = this.workingDirectory;
        myProcess.Start();
        myProcess.WaitForExit();
    }
}

この方法で呼び出すと、次のようになります。

private void RunCommands()
{
  var command = "LongRunCommand.exe";
  string binDirectory = Path.Combine(FileSystem.ApplicationDirectory, binFolderName);
  var cp = new CommandProcessor(this.workingDirectory, binDirectory);
  for(int i=0;i<10;i++)
  {
       cp.RunCommand(Command, "-i {0}", i);
  }
 }   

上記のコードは、直接呼び出しの一部として呼び出され、アプリケーションをブロックします (アプリケーションはこのプロセス中にハングするようです。

ハングアップの問題を解決するために、次のようにバックグラウンドワーカーを使用しました。

   var worker = new BackgroundWorker();
   worker.DoWork += this.WorkerDoWork;
   worker.RunWorkerCompleted += this.workerRunWorkerCompleted;
   worker.RunWorkerAsync();

WorkerDoWork 内で runco​​mmand を呼び出します。

この行を呼び出した後、アプリケーションは終了しました。

 myProcess.WaitForExit();

デバッグ情報はなく、終了コードは -1 です。

何が問題で、どうすれば解決できますか?

BackgroundWorker を使用せずに目標を達成するためのより良い方法はありますか?

4

1 に答える 1