私は、私がBackgroundWorkers(多くの異なる操作)を実行するときに表示されるようにしたい私のGUIのコントロールを持っています。これらの操作の中には500ms未満のものがありますが、私はコントロールを短時間で見えるようにすることは役に立たないと感じています。したがって、私はBackgroundWorkerが既に500msの間働いていた場合にのみ、コントロールを可視にしたいと思います。BackgroundWorker - レポート時間
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の内部のタイマーを使用し、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);
}
}
関連する問題
- 1. ハイブで4時間レポート
- 2. Cron Job PHPスクリプト実行時間レポート
- 3. XtraSchedulerコントロールによる合計時間レポート
- 4. 「無駄な時間」レポートEclipseプラグイン?
- 5. エージェントによるTFS構築時間レポート
- 6. Youtrack時間レポート共同作業者の1日あたりの時間数
- 7. 外部クラスからのBackgroundWorkerレポートの進捗状況?
- 8. 2つのプログレスバーのBackgroundworkerレポートの進捗状況
- 9. Backgroundworker abort
- 10. Excelレポート生成に長時間を要する
- 11. Crystalレポートの読み込み時間が長くなる
- 12. acumaticaレポートの日付時間を印刷するには?
- 13. JUnit障害レポートから時間値を除外する
- 14. BackgroundWorker + WebBrowser
- 15. BackgroundWorkerの支援キャンセル
- 16. クロージャとBackgroundWorkerイベントハンドラ
- 17. BackgroundWorkerスレッドとタイマーロジック
- 18. BackgroundWorkerのC#の
- 19. BackgroundWorker In Form.Loadイベント
- 20. C#Winform ProgressBarとBackgroundWorker
- 21. ピクセルモニタとしてのBackgroundWorker - C#
- 22. Windows Mobile BackgroundWorker
- 23. C#Backgroundworker ConsoleWriteline
- 24. BackgroundWorkerの問題
- 25. BackgroundWorkerとClipBoard
- 26. BackgroundWorkerとWPF
- 27. BackgroundWorkerとスレッド
- 28. backgroundworker vsイベント
- 29. wpfスレッド、backgroundworker、ディスパッチャ
- 30. BackgroundWorkerのSpeechRecognitionEngine
あなたはどのようにタイマーを使用する方法を教えてもらえますか? Backgroundworkerの残りの部分の実行をブロックしていないのですか?私は、Backgroundworkerの実際の作業が500ms以上続く場合にのみ、毎回表示されるMyControlを持っていません。 – santBart
Timerは別のスレッドで実行され、あなたのBakgroundWorkerをブロックする。 BackgroundWorker1ProgressChangedイベントハンドラの中で、自分のコントロールで何をすべきかを決めることができます。 – Strillo