我在GUI上有一個控件,我只想在運行BackgroundWorkers(許多不同的操作)時使其可見。其中一些操作持續時間不到500毫秒,我覺得在短時間內使控件可見是沒有用的。因此,我希望只有在BackgroundWorker已經工作了500毫秒時才能使控件可見。BackgroundWorker - Report Time
1
A
回答
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);
}
}
相關問題
- 1. SQL Server Report Builder:連接數據類型「date」和「time」
- 2. 使用C的BackgroundWorker#
- 3. crystal report&sub report
- 4. BackGroundWorker
- 5. Crystal Report - Report Header Section
- 6. Srss Report #error
- 7. 的BackgroundWorker取消
- 8. 從一個BackgroundWorker
- 9. SSRS 2008 Report Report Rendering Question
- 10. Cyrstal Report Sub Report分頁
- 11. BackgroundWorker中的SQL查詢
- 12. time javascript
- 13. BackgroundWorker的
- 14. SpeechRecognitionEngine BackgroundWorker
- 15. Powershell Backgroundworker
- 16. BackgroundWorker - C#
- 17. Multi-BackgroundWorker
- 18. Backgroundworker abort
- 19. Ruby on Rails Time In/Time Out Records
- 20. 爲什麼cc「real time」>>「real time」?
- 21. BackgroundWorker:將通用列表傳遞給BackgroundWorker
- 22. 從BackgroundWorker DoWork傳遞值到BackgroundWorker完成
- 23. WFP BackgroundWorker等待其他BackgroundWorker完成
- 24. BackgroundWorker中的BackgroundWorker - 錯誤處理
- 25. 從另一個backgroundworker c停止backgroundworker#
- 26. BackgroundWorker和foreach循環
- 27. 取消backgroundworker操作
- 28. Backgroundworker保持忙碌
- 29. Joda-Time for Javascript?
- 30. TextBox for Time
你能告訴我怎麼使用定時器?是不是阻止Backgroundworker的rest dowork部分的執行?我不會每次都出現MyControl,只有在Backgroundworker的實際工作持續時間超過500ms的情況下 – santBart 2011-12-13 23:07:07