0

こんばんは、

C# をいじり始めたばかりで、コマンド ラインで実行されるプログラムの GUI を作成しようとしました。実行できましたが、進行状況バーを実装しようとして立ち往生しています。

他の投稿を読みましたが、正確な問題が見つからないか、解決策を問題に適用する方法を理解できません。

これが私のコードです(これが非常に面倒な場合はお詫びします):

private void MethodToProcess(Object sender, DoWorkEventArgs args)
    {
        // Set all the strings for passthrough
        String USMTPath_Work = USMTPath + USMTArch;
        String USMTPath_full = USMTPath_Work + @"\Scanstate.exe";
        String USMTFlags_Capture = @"/c /v:13 /o /l:scanstate.log /localonly /efs:copyraw";
        String Argument_full = SavePath + XML1 + XML2 + USMTFlags_Capture;

        // Test that USMT path is correct            
        if (USMTPath == null)
        {
            MessageBox.Show("Error: There is no USMT Path defined.");
            return;
        }

        // Test that Windows folder is correct when offline
        /* if (Windows_Path == null)
        {
            MessageBox.Show("Error: There is no Windows Path to capture.");
            return;
        } */

        // Runs the capture
        System.Diagnostics.Process Scanstate = new System.Diagnostics.Process();
        Scanstate.StartInfo.FileName = USMTPath_full;
        Scanstate.StartInfo.Arguments = Argument_full;
        Scanstate.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        Scanstate.StartInfo.WorkingDirectory = USMTPath_Work;
        //Scanstate.StartInfo.UseShellExecute = false;
        Scanstate.StartInfo.CreateNoWindow = true;
        //Scanstate.StartInfo.RedirectStandardOutput = true;
        Scanstate.Start();
        Scanstate.WaitForExit();

        String Str_ExitCode = Scanstate.ExitCode.ToString();

        if (Scanstate.ExitCode == 1)
            MessageBox.Show("Error: Data has not been captured. Please check the log files for details.");
        if (Scanstate.ExitCode == 0)
            MessageBox.Show("Success: Data has been captured. For more information, check log files.");
        else
        {
            MessageBox.Show("Error: Unknown error has occurred. Please check the log files for details.");
            MessageBox.Show("Error Code: " + Str_ExitCode);
        }

        Scanstate.Close();

    }

基本的に、プロセス scanstate.exe を実行しようとしています。現在、進行状況を取得してプログレスバーに渡すことができるように、backgroundworker を実行しようとしています。

 private void btnCapture_Click(object sender, EventArgs e)
    {
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 100;
        progressBar1.Step = 1;

        BackgroundWorker CaptureBG = new BackgroundWorker();
        CaptureBG.WorkerReportsProgress = true;
        CaptureBG.DoWork += new DoWorkEventHandler(MethodToProcess);
        CaptureBG.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(CaptureBG_RunWorkerCompleted);
        CaptureBG.ProgressChanged += new ProgressChangedEventHandler(CaptureBG_ProgressChanged);
        CaptureBG.RunWorkerAsync();
    }

        private void CaptureBG_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
    {
        progressBar1.Value = 100;
    }

    private void CaptureBG_ProgressChanged(object sender, ProgressChangedEventArgs args)
    {
        progressBar1.Value++;
    }

ただし、プロセスが実行されているため、使用法を誤解しているか、何かが欠けていますが、プログレスバーで進行状況が得られません。プロセスが終了すると、それは満たされます。

私は何を間違っていますか?一般的に、プロセスにかかる時間が正確にわからない場合、プロセスはどのように進行状況を報告しますか?

前もって感謝します

4

1 に答える 1

0

BackgroundWorkerは、そのタスクがさらに完了するにつれて、進行状況を更新する責任があります。

起動するプロセスと、そのプロセスの進行状況をコードに戻すコードとの間に相互作用はありません。

これが機能するためには、次の 2 つのことが必要です。

  1. プロセスがBackgroundWorkerに進行状況を報告するメカニズムを定義する必要があります。
  2. ProgressChangedイベントが発生するように、BackgroundWorkerはReportProgressメソッドを呼び出して自身の進行状況を更新する必要があります。

最初のステップは難しいもので、scanstate.exeがどのように機能するかによって異なります。コンソールへの書き込みなど、進行状況を示すために何かを行いますか? その場合は、コンソール出力をリダイレクトし、その出力を解析して進行状況を判断するか、少なくとも推定することができます。

アップデート

Scanstate.exeは、進行状況をログに書き込む機能を提供します。次に例を示します。

scanstate /i:migapp.xml /i:miguser.xml \\fileserver\migration\mystore /progress:prog.log /l:scanlog.log

BackgroundWorker でFileWatcherを使用して、進行状況ログの変更を探し、それに応じて進行状況を更新できます。

于 2012-08-07T03:46:16.710 に答える