Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Backgroundworker?_C# - Fatal编程技术网

C# 如何使用Backgroundworker?

C# 如何使用Backgroundworker?,c#,C#,我需要在启动“cleanmgr”时显示一个进程栏。我到处搜索,有很多代码,但没有任何帮助。请帮帮我 private void button1_Click(object sender, EventArgs e) { var Proc = new Process(); Proc.SynchronizingObject = this; Proc.EnableRaisingEvents = true; Proc.StartInfo.

我需要在启动“cleanmgr”时显示一个进程栏。我到处搜索,有很多代码,但没有任何帮助。请帮帮我

private void button1_Click(object sender, EventArgs e)
    {
        var Proc = new Process();
        Proc.SynchronizingObject = this;
        Proc.EnableRaisingEvents = true;
        Proc.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%windir%\system32\cleanmgr.exe");
        Proc.StartInfo.Arguments = @"/Sagerun:100";
        Proc.StartInfo.Verb = "runas";
        Proc.StartInfo.CreateNoWindow = true;
        Proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        Proc.Start();
        backgroundWorker1.RunWorkerAsync(Proc);
        int i = 0;
        Process[] Proc1 = Process.GetProcessesByName("cleanmgr.exe");
        i++;
        toolStripProgressBar1.Value.ToString() + "%";

        backgroundWorker1.ReportProgress(i);
        if ((backgroundWorker1.CancellationPending == true))
        {
            e.ToString = true;
        }

您可以使用
ProgressChanged
事件触发

backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
//Add the event to see the ProgressChanged
backgroundWorker1.ReportProgress(i);
在这种情况下,您可以在进度条上随心所欲地工作

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

此时,我无法找到将我的进度条与另一个进度条软件的进度条同步的答案。我选择了另一种解决方案。我选择了带有“EventHandler”的事件


你能实际衡量这个过程的进展吗?如果你不能测量它,你怎么能向用户显示它呢?目前,我无法测量它,因为它取决于用户计算机的速度。所以我把一个相对的度量或者。。。它是一个Windows清理工具。但是,它有自己的进度条。是否可以将其与我自己的bare同步?如何从外部应用程序的进度条中获取值是一个非常不同的问题,您应该首先自己研究。在此之前,最合适的选择是将您自己的
ProgressBar
样式设置为
Marquee
,以指示正在发生的事情,但不是实际发生的地方,因为您不知道。
public Form1()
    {
        InitializeComponent();
        this.CenterToScreen();
        toolStripProgressBar1.Style = ProgressBarStyle.Continuous;
        button1.Click += new EventHandler(BarAnimation);
        button2.Click += new EventHandler(BarAnimation);
        button3.Click += new EventHandler(BarAnimation);
        button4.Click += new EventHandler(BarAnimation);
    }
private void button1_Click(object sender, EventArgs e)
    {
        var Proc = new Process();
        Proc.SynchronizingObject = this;
        Proc.EnableRaisingEvents = true;
        Proc.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%windir%\system32\cleanmgr.exe");
        Proc.StartInfo.Arguments = @"/sagerun:100";
        Proc.StartInfo.Verb = "runas";
        Proc.StartInfo.CreateNoWindow = true;
        Proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        Proc.Start();
        Proc.Exited += new EventHandler(ProcExited); 
    private void BarAnimation(object sender, EventArgs e)
    {
        toolStripProgressBar1.MarqueeAnimationSpeed = 1;
        toolStripProgressBar1.Style = ProgressBarStyle.Marquee;
    }

    private void ProcExited(object sender, EventArgs e)
    {
        toolStripProgressBar1.Style = ProgressBarStyle.Continuous;
    }