0

フォームの上部で私がした:

progressBar1.Maximum = 100;
progressBar1.Minimum = 1;

次に、操作を開始するボタンクリックイベントで、次のことを行いました。

timer2.Enabled = true;
if (this.backgroundWorker1.IsBusy == false)
            {
                this.backgroundWorker1.RunWorkerAsync();
            }

次に、backgroundworkerdowork イベントで:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            if (filesContent.Length > 0)
            {
                for (int i = 0; i < filesContent.Length; i++)
                {
                    File.Copy(filesContent[i], Path.Combine(contentDirectory, Path.GetFileName(filesContent[i])), true);
                }
            }
            DoProgressBar(e, worker);
            WindowsUpdate();
            CreateDriversList();
            GetHostsFile();
            Processes();
        }

DoWork イベントのすべての関数はファイルをコピーしています。Processes() 関数は、Process を使用してファイルを作成/コピーする新しいクラスを使用します。

次に、私が行った新しい DoProgressBar イベント関数:

private static void DoProgressBar(DoWorkEventArgs e, BackgroundWorker worker)
        {
            for (int i = 1; i <= 90; i++)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    // Perform a time consuming operation and report progress.
                    System.Threading.Thread.Sleep(50);
                    worker.ReportProgress(i);
                }
            }
        }

次に ProgressChanged イベント:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

次に、完了したイベント:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                this.label1.Text = "Canceled!";
            }
            else if (!(e.Error == null))
            {
                this.label1.Text = ("Error: " + e.Error.Message);
            }
            else
            {
                this.progressBar1.Value = this.progressBar1.Maximum;
                processfinish = true;
            }
        }

Timer2 ティック イベント:

private void timer2_Tick(object sender, EventArgs e)
        {
            timerCount += 1;
            TimerCount.Text = TimeSpan.FromSeconds(timerCount).ToString();
            TimerCount.Visible = true;
            if (processfinish == true)
            {
                timer2.Enabled = false;
                timer1.Enabled = true;
            }                           
        }

そして timer1 tick イベント:

private void timer1_Tick(object sender, EventArgs e)
        {
            count++;
            Diagnose.Text = "PROCESS HAS FINISHED" + "  " + countBack--;
            if (count == 6)
            {
                Diagnose.Text = "COLLECT INFORMATION";
                Diagnose.Enabled = true;
                CreateZip.Enabled = true;
                ViewLogFile.Enabled = true;
                DriverVerifier.Enabled = true;
                timer1.Enabled = false;
                TimerCount.Visible = false;
            }
        }

長いコードであることは承知していますが、ここにあるものはすべてつながっています。

私がやりたかったのは、DoWork イベントの各関数の進行状況に応じて、progressBar が進行するようにすることです。

しかし、代わりに今何をしているのかは、最初に に行くことです:

DoProgressBar() イベント/関数は、2 番目/その他の部分を実行します ReportProgress(i)

次に、Progresschanged イベントに移動して、次のことを行います。

結果は、ボタンをクリックして操作を開始すると、プログラムの各機能/進行状況に応じて移動する代わりに、進行状況バーがほぼ最後まで移動することがすぐにわかります。

ここで Form1 の完全なコードを確認できます。

http://codepaste.net/fuk9w5

編集:

これは、関数 Processes() の Form1 で使用しているクラス ProcessRun のコードです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;

namespace Diagnostic_Tool_Blue_Screen
{
    class ProcessRun
    {


        public void ProcessesRun()
        {

        }

        public static void Processing(string WorkingDirectory, string FileName, string Arguments, bool StandardOutput, string OutputFileName)
        {
            Process proc = new Process();
            proc.EnableRaisingEvents = true;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = StandardOutput;
            proc.StartInfo.FileName = FileName;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.WorkingDirectory = WorkingDirectory;
            proc.StartInfo.Arguments = Arguments;
            proc.Start();
            if (StandardOutput == true)
            {
                string output = proc.StandardOutput.ReadToEnd();
                DumpOutput(WorkingDirectory + "\\" + OutputFileName, output);
            }
            proc.WaitForExit();
            proc.Close();
        }

        private static void DumpOutput(string filename, string output)
        {
            StreamWriter w = new StreamWriter(filename);
            w.Write(output);
            w.Close();
        }
    }
}
4

3 に答える 3

0

クロススレッドで進捗バーの値を変更する方法を見つけようとしているときにここにたどり着いた人は、次のようにします。

form.Invoke((Action)delegate { form.function(); });
于 2016-05-03T16:11:32.950 に答える