I am writing an application that requires updating the status bar while the database is loading. I set the label to "Loading Database ...", then downloaded the database using BackgroundWorker. When the employee finishes work, I set the label "Database is loaded." However, this is done only at startup, and I do not want the label to be visible much longer, and I would like it to clear a few seconds after the employee finishes. I could place the highlighted timer object on my main, but this only action would be its only work, and it seems that a more elegant solution should exist. So I tried lambda:
void dataLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DataLabel.Text = "Database Loaded";
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 5000;
timer.Tick += new EventHandler((o, a) =>
{
DataLabel.Text = "";
(o as System.Windows.Forms.Timer).Enabled = false;
});
}
Of course, the space timerexpires after the function exits, and the event is Ticknever called.
How can I get a simple one-time event to fire without using global instance timers?
source
share