2016-12-08 1 views
-1

"cleanmgr"を起動するときにプロセスバーを表示する必要があります。私はどこでも検索しますが、コードはたくさんありますが、私を助けるものはありません。私を助けてください。Backgroundworkerの使い方は?

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; 
     } 
+0

あなたは実際にはそのプロセスの進捗状況を測定することはできますか?あなたがそれを測定できない場合、どのようにユーザーに何かを表示することができますか? – jmcilhinney

+0

この時点では、ユーザーのコンピュータの速度に依存するため、私はそれを測定することができません。だから私は相対的な尺度を置くか...それはWindowsのクリーンアップユーティリティです。 –

+0

しかし、独自の進行状況バーがあります。それを私の裸のものと同期させることは可能ですか? –

答えて

0

ProgressChangedイベントを発生させることができます。

あなたが好きなようにプログレスバーを操作することができます。

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

現時点では、プログレスバーと別のプログレスバーソフトウェアとの同期のための回答が見つかりませんでした。私は別の解決策を選んだ。私は "EventHandler"でイベントを選択しました。

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; 
    }