I have a Windows.Forms.Timer used in a form that works if I set its Enabled property to true in the properties window, and that is the only time it works. If I leave it disabled then enable it only when I need to, it doesn’t tick.
private void btnRename_Click(object sender, EventArgs e)
{
timerUpdateProgress.Enabled = true;
timerUpdateProgress.Start();
pbProgress.Maximum = clbFiles.CheckedItems.Count;
var renameTask = Task.Factory.StartNew(() => doRename(true, tbCurrentDirectory.Text, clbFiles.CheckedItems, rules));
if(renameTask.Result.Count > 0)
{
timerUpdateProgress.Enabled = false;
new ExceptionsWindow(renameTask.Result).ShowDialog();
}
timerUpdateProgress.Enabled = false;
loadFiles(tbCurrentDirectory.Text);
}
private void timerUpdateProgress_Tick(object sender, EventArgs e)
{
pbProgress.Value = progress; //I have a breakpoint on this line
}
All I’m trying to do is display the progress of an operation with a progress bar. Enabling and starting the timer does nothing, the tick never happens. Why is this happening?
Update: stepping through the code after adding a sleep of 2000ms after enabling and starting the timer shows it still does not tick (I have a breakpoint in the tick handler).
More clarification: When the timer is enabled in the property window, the tick handler is always being called, and that’s without calling Start(). My breakpoint is triggered all the time just by enabling the timer. I should also note that I’m using .NET 4.
EDIT:
After getting an idea from the comments – it seems you do not need the timer at all.
You can update the progress bar
Valueas and when the value ofprogressis updated.That way the transition from 0 – 100% might also seem smoother.
According to me you need disable the timer.
Just use
And then
timer.Start()when needed again.Additionally, if you have disabled timer progress, then you also must do