2011-12-13 12 views
1

私は、私がBackgroundWorkers(多くの異なる操作)を実行するときに表示されるようにしたい私のGUIのコントロールを持っています。これらの操作の中には500ms未満のものがありますが、私はコントロールを短時間で見えるようにすることは役に立たないと感じています。したがって、私はBackgroundWorkerが既に500msの間働いていた場合にのみ、コントロールを可視にしたいと思います。BackgroundWorker - レポート時間

答えて

1

ちょうどあなたはBGWを開始すると同時に、タイマーを起動します。

private void button1_Click(object sender, EventArgs e) { 
     timer1.Enabled = true; 
     backgroundWorker1.RunWorkerAsync(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) { 
     timer1.Enabled = false; 
     myControl1.Visible = true; 
    } 

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { 
     timer1.Enabled = myControl1.Visible = false; 
    } 
0

BackgroundWorkerReportProgressメソッドを利用してください。状態パラメータに任意の値を置き、それに応じて計算を実行することができます。

public class MyObject 
{ 
    public DateTime TimeStarted {get; set;} 
} 

は、その後、あなたのProgressChangedイベントハンドラで...

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    if(DateTime.Now.Subtract(((MyObject)e.UserState).TimeStarted).TotalMilliseconds > 500) 
    { 
     //show your control 
    } 
} 
0

あなたはBackgroundWorkerの内部のタイマーを使用し、500ミリ秒が経過した後ReportProgressメソッドを呼び出すことができます。

UIスレッドでは、ProgressChangedイベントを処理し、必要に応じてコントロールを表示/非表示にするだけです。

public partial class Form1 : Form 
{ 
    /// <summary> 
    /// Timer. 
    /// </summary> 
    private Timer timer = new Timer(); 

    /// <summary> 
    /// Initializes a new instance of the <see cref="Form1"/> class. 
    /// </summary> 
    public Form1() 
    { 
     InitializeComponent(); 

     backgroundWorker1.WorkerReportsProgress = true; 
     backgroundWorker1.DoWork += BackgroundWorker1DoWork; 
     backgroundWorker1.ProgressChanged += BackgroundWorker1ProgressChanged; 

     timer.Interval = 500; 
     timer.Tick += TimerTick; 
    } 

    /// <summary> 
    /// Handles the Tick event of the timer control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
    void TimerTick(object sender, EventArgs e) 
    { 
     timer.Enabled = false; 
     backgroundWorker1.ReportProgress(99); 
    } 

    /// <summary> 
    /// Handles the Click event of the button1 control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
    private void Button1Click(object sender, EventArgs e) 
    { 
     timer.Enabled = true; 
     backgroundWorker1.RunWorkerAsync(); 
    } 

    /// <summary> 
    /// Handles the DoWork event of the backgroundWorker1 control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param> 
    private void BackgroundWorker1DoWork(object sender, DoWorkEventArgs e) 
    { 
     BackgroundWorker worker = sender as BackgroundWorker; 

     // Do your work... 
     Thread.Sleep(2000); 
    } 

    /// <summary> 
    /// Handles the ProgressChanged event of the backgroundWorker1 control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.ComponentModel.ProgressChangedEventArgs"/> instance containing the event data.</param> 
    private void BackgroundWorker1ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     label1.Visible = (e.ProgressPercentage == 99); 
    } 
} 
+0

あなたはどのようにタイマーを使用する方法を教えてもらえますか? Backgroundworkerの残りの部分の実行をブロックしていないのですか?私は、Backgroundworkerの実際の作業が500ms以上続く場合にのみ、毎回表示されるMyControlを持っていません。 – santBart

+0

Timerは別のスレッドで実行され、あなたのBakgroundWorkerをブロックする。 BackgroundWorker1ProgressChangedイベントハンドラの中で、自分のコントロールで何をすべきかを決めることができます。 – Strillo

関連する問題