2012-03-10 10 views
2

私は仕事の進行状況を取得できません!コードはReportProgress doesntのを実行されます場合でも、私はバーが空のままで次のコードを実行した場合に何を更新するように見える..:あなたの助けを事前にbackgroundWorkerとprogressChangedが動作しない

namespace GPUZ_2 

{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     GPUZdata test = new GPUZdata 
     { 
     }; 

     //invio l'oggetto al thread backgroundworker 
     backgroundWorker1.RunWorkerAsync(test); 
    } 


    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     // 
     // e.Argument always contains whatever was sent to the background worker 
     // in RunWorkerAsync. We can simply cast it to its original type. 
     // 
     GPUZdata argumentTest = e.Argument as GPUZdata; 




     argumentTest.OneValue = 6; 
     Thread.Sleep(2000); 
     backgroundWorker1.ReportProgress(50); 
     argumentTest.TwoValue = 3; 
     Thread.Sleep(2000); 
     backgroundWorker1.ReportProgress(100); 


     // 
     // Now, return the values we generated in this method. 
     // Always use e.Result. 
     // 
     e.Result = argumentTest; 
    } 


    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 

     // Receive the result from DoWork, and display it. 

     GPUZdata test = e.Result as GPUZdata; 
     this.Text = test.OneValue.ToString() + " " + test.TwoValue.ToString(); 

    } 

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     // Change the value of the ProgressBar to the BackgroundWorker progress. 
     progressBar1.Value = e.ProgressPercentage; 
     // Set the text. 
     this.Text = e.ProgressPercentage.ToString(); 
    } 
} 

は}

おかげ

+2

最も重要なことは、あなたは何をする必要があります。RunWorkerCompletedイベントに* * e.Errorプロパティを無視することはありません。 –

+0

okありがとう、私はちょうど必要最小限のコードでチュートリアルに従っていた – rekotc

答えて

5

のBackgroundWorkerを初期化するには、進捗レポートを有効にする必要がありますイベントハンドラを作成して接続してください:

// Enable progress reporting 
backgroundWorker1.WorkerReportsProgress = true; 

// Hook up event handlers 
backgroundWorker1.DoWork += backgroundWorker1_DoWork; 
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted; 
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged; 
+0

ありがとう!これは私の問題を解決した、私はそれを実行するために、イベントハンドラをつかまえなければならなかったことを理解していない、ありがとう:) – rekotc

3

IドンあなたがtrueにWorkerReportsProgressプロパティを設定する場所「tは見る - 最も可能性の高い問題である:

backgroundWorker1.WorkerReportsProgress = true; 
backgroundWorker1.RunWorkerAsync(test); 
+0

私はコードに行を追加しようとしましたが、とにかく私はビジュアルスタジオのGUIを使用して進捗報告を有効にした、それは同じですか? – rekotc

-1

私は同じ問題がありました。 AssemblyInfo.csでは、ComVisibleに対してこの変更を行う必要があります。

[assembly: ComVisible(true)] 
+0

あなたの答えを正当化する詳細を提供できますか? ComVisible(真)が問題を解決する理由など –

関連する問題