santBart
santBart

Reputation: 2496

BackgroundWorker - Report Time

I have a control on my GUI which I would like to make visible only case when I run BackgroundWorkers (many different operations). Some of these operations last less than 500ms, and I feel that making the control visible for so short a time is useless. Therefore, I would like to make the control visible only if a BackgroundWorker has already been working for 500ms.

Upvotes: 1

Views: 662

Answers (3)

Strillo
Strillo

Reputation: 2982

You can use a Timer inside the BackgroundWorker and call the ReportProgress method once 500ms have passed.

In the UI thread you then just need to handle the ProgressChanged event and show/hide your control as required.

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

Upvotes: 0

Aaron McIver
Aaron McIver

Reputation: 24723

Leverage the ReportProgress method on the BackgroundWorker. You can place whatever you want in the state parameter and then perform the calculation accordingly.

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

Then in your ProgressChanged event handler...

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

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942548

Just start a timer at the same time you start the 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;
    }

Upvotes: 1

Related Questions