how to work timer in background
i can run timer but it is hanging , when i run background i need timer to be run in background. can anyone say me how to run timer in background. My timer code is
btnIntraday.Enabled = false;
btnStartBackfill.Enabled = false;
btnStop.Enabled = true;
if (btnIntraday.Text == "Intraday")
{
timerIntraday.Interval = 5000;
timerIntraday.Enabled = true;
btnIntraday.Text = "Updating..";
}
else if (btnIntraday.Text == "Updating..")
{
timerIntraday.Enabled = false;
btnIntraday.Text = "Intraday";
}
and my background code is
btnIntraday.Enabled = false;
btnStartBackfill.Enabled = false;
btnStop.Enabled = true;
txtInterval.Text = ddTimeInterval.Value.ToString();
int inter = (int.Parse(txtInterval.Text)) * multiplyingFactorBackfill;
try
{
bgBackfillDCX.RunWorkerAsync();
}
catch (Exception ex)
{
}
can anyone please say me how to run 开发者_如何学JAVAtimer in background. Thanks in advance.
You may use a BackgroundWorker.
Handle the BackgroundWorker.DoWork to run your timer.
Handle the BackgroundWorker.ProgressChanged to handle timing events.
Handle the BackgroundWorker.RunWorkerCompleted to stop the timer.
I think what you are saying is that you want to set off the background code then have IT periodically do some work. If so you need a timer in the background code not the front end. Which means using a different timer class - in System.Timers, IIRC. System.Timers.Timer
If You insist on having the timer working at the front end (though I don't recommend it, I can understand it is easier in some scenarios), there is a way (note: I presume Windows Forms are used):
In the following code, there is:
- a timer referring to an instance of System.Windows.Forms.Timer object
- a notifyIcon referring to an instance System.Windows.Forms.NotifyIcon object
- MainWindow_Resize method associated with Resize event of the form
- notifyIcon_Click method associated with Click event of the notifyIcon
The resize method if-block is executed when user minimizes form (form is not visible anywhere in desktop and setting ShowInTaskBar to false hides it from task bar as well, so it is effectively hidden).
private void MainWindow_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
notifyIcon.Visible = true;
}
}
When user clicks notification icon, the form's window is restored to its former size and position and the form is brought to user
private void notifyIcon_Click(object sender, EventArgs e)
{
notifyIcon.Visible = false;
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.BringToFront();
this.Activate();
}
精彩评论