BackgroundWorker - 报告时间

时间:2011-12-13 22:53:24

标签: c# multithreading backgroundworker

我在GUI上有一个控件,我想在运行BackgroundWorkers(许多不同的操作)时只能看到它。其中一些操作持续时间不到500毫秒,我觉得让控件可见这么短的时间是没用的。因此,我想只有在BackgroundWorker已经工作500ms时才能使控件可见。

3 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

利用BackgroundWorker上的ReportProgress方法。您可以在state参数中放置任何所需的内容,然后相应地执行计算。

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

答案 2 :(得分:0)

您可以在BackgroundWorker中使用Timer,并在500ms过后调用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);
    }
}