2011-12-13 54 views
1

我在GUI上有一個控件,我只想在運行BackgroundWorkers(許多不同的操作)時使其可見。其中一些操作持續時間不到500毫秒,我覺得在短時間內使控件可見是沒有用的。因此,我希望只有在BackgroundWorker已經工作了500毫秒時才能使控件可見。BackgroundWorker - Report Time

回答

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

利用BackgroundWorker上的ReportProgress方法。你可以在狀態參數中放置任何你想要的,然後相應地進行計算。

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的內部定時器,並調用ReportProgress方法一旦爲500ms已經過去了。

在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的rest dowork部分的執行?我不會每次都出現MyControl,只有在Backgroundworker的實際工作持續時間超過500ms的情況下 – santBart 2011-12-13 23:07:07