2017-10-09 10 views
0

このトピックに関する多くの記事やその他の質問を読んだことがありますが、C#の初心者で一般的なスレッドを扱っているので、回答は悲しいことにほとんど私にとっては複雑で、私の問題。別のスレッドの進行状況バーを更新する

私は、ほとんど0.03%の非常に小さなステップで満たされるこのプログレスバーを持っています。これを普通に含めると、毎回実行され、毎回0.03%のステップで更新されます。私は、別のスレッドで進捗バーを更新することは、トリックを行うだろうと信じていますが、私は悲しいことに、このトピックを理解することができます。

public void CopyAll(DirectoryInfo source, DirectoryInfo target) 
{ 
    string source4count = source.ToString(); 
    if (checkSubdirCase == 0) 
    { 
     allfiles = System.IO.Directory.GetFiles(source4count, "*.*", System.IO.SearchOption.AllDirectories); 
     allFilesNum = allfiles.Length; 
     progressbarinterval = 0;      
     progressbarinterval = 100/allFilesNum; 
     Progressbarvalue = 0; 
    } 

    if (Directory.Exists(target.FullName) == false) 
    { 
     Directory.CreateDirectory(target.FullName); 
    } 

    foreach (FileInfo fi in source.GetFiles()) 
    { 
     //HERE IS THE UPDATING OF THE PROGRESSBAR 
     fi.CopyTo(System.IO.Path.Combine(target.ToString(), fi.Name), true); 
     Progressbarvalue = Progressbarvalue + progressbarinterval;      
     ProgressBarCopy.Value = Progressbarvalue; 
    } 
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) 
    { 
     DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); 
     checkSubdirCase = 1; 
     CopyAll(diSourceSubDir, nextTargetSubDir); 
    } 
} 

私はsomeeoneが私を助け、これに対処するための方法および背景の労働者が実際にどのように機能するかを簡単に説明することを願って:

この

は私のプログレスバーが更新されるコードセクションです。 よろしく

答えて

2

あなたは、おそらくこのような背景の労働者(これはUIスレッド上で発生)を作成することになるでしょう:あなたのDoWork-方法で

var worker = new BackgroundWorker(); 
worker.WorkerReportsProgress = true; 
worker.DoWork += new DoWorkEventHandler(WorkerDoWork); 
worker.ProgressChanged += new ProgressChangedEventHandler(WorkerProgressChanged); 

を、あなたは現在COPYALLで行う全力を尽くします-methodでは、進捗処理だけが変更されます。 worker.ReportProgress(...)を呼び出す必要があります。これがUIスレッドにマーシャリングされ、WorkerProgressChangedメソッドが呼び出されます。 WorkerProgressChangedメソッドでは、進行状況バーを更新できます。

ただし、UIスレッドとBackgroundWorkerスレッドの切り替えがかなり高価であることに注意する必要があります。実際にUIに影響を与える量だけ進捗が報告された場合のみ進捗を報告してください。

+2

この回答はありがとうございます。それは容易に理解でき、私は今それを試してみるでしょう。 :) – MansNotHot

2

他の解決策は、ProgressBar

変更使用してボタン

//CopyAll(); //Old 
Task.Run(() => CopyAll()); 

ボークとあなたの方法

public void CopyAll(DirectoryInfo source, DirectoryInfo target) 
{ 
    string source4count = source.ToString(); 
    if (checkSubdirCase == 0) 
    { 
     allfiles = System.IO.Directory.GetFiles(source4count, "*.*", System.IO.SearchOption.AllDirectories); 
     allFilesNum = allfiles.Length; 
     progressbarinterval = 0;      
     progressbarinterval = 100/allFilesNum; 
     Progressbarvalue = 0; 
    } 

    if (Directory.Exists(target.FullName) == false) 
    { 
     Directory.CreateDirectory(target.FullName); 
    } 

    foreach (FileInfo fi in source.GetFiles()) 
    { 
     //HERE IS THE UPDATING OF THE PROGRESSBAR 
     fi.CopyTo(System.IO.Path.Combine(target.ToString(), fi.Name), true); 
     Progressbarvalue = Progressbarvalue + progressbarinterval;      
     ProgressBarCopy.Invoke(new MethodInvoker(delegate { ProgressBarCopy.Value = Progressbarvalue; })); 
    } 
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) 
    { 
     DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); 
     checkSubdirCase = 1; 
     CopyAll(diSourceSubDir, nextTargetSubDir); 
    } 
} 
+0

私は、Task.run() - > CopyAll();と思っていますが、これはどこに置かれ、何をしますか?UPATE:入手しました私のdevExプログレスバーは "起動"しているようですが、 – MansNotHot

+0

まだエラーメッセージが表示されます:progressbar does notは呼び出しの定義を含んでいます。問題点を知りません。 – MansNotHot

+0

UPDATE:見つかりました。 – MansNotHot

1

上のコードの呼び出しを持つタスクの使用でありますバックグラウンドワーカーは良い選択です。

var worker = new BackgroundWorker(); 
worker.DoWork += new DoWorkEventHandler(Worker_DoWork); 
worker.ProgressChanged += new 
ProgressChangedEventHandler(Worker_ProgressChanged); 
worker.RunWorkerCompleted += new 
RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted); 


private void Worker_DoWork(object sender, DoWorkEventArgs e) 
{ 

... 
    foreach (FileInfo fi in source.GetFiles()) 
    { 
    //Copying the files.. 

    // Calling the ReportProgress method would fire the worker_ProgressChanged event 
    worker.ReportProgress(0, progressState) 
} 


    } 

    private void worker_ProgressChanged(object sender, 
             ProgressChangedEventArgs e) 
    { 
     // This is where you would have the UI related changes. 
     //In your case updating the progressbar. 
     // While the files are being copied this would update the UI. 
    } 

private void Worker_RunWorkerCompleted(object sender, 
RunWorkerCompletedEventArgs e) 
{ 
worker.CancelAsync(); 

} 
関連する問題