2
In my application, I created a "Timer" (System.Windows.Forms) that runs every 1 second. In the "tick" event, I placed a await Task.Run.
For some reason the tick stops running after a while (because it doesn’t print the date and time on the console).
Class x {
public Timer timer {get; set;}
public void f()
{
timer = new Timer();
timer.Tick += new EventHandler(this.tick);
timer.Enabled = false;
timer.Interval = 1000;
timer.Start();
}
private async void tick(object sender, EventArgs e)
{
this.status = ProcessoStatus.TRABALHANDO;
this.timer.Stop();
try
{
await Task.Run(() => this.processo());
}
catch
{}
this.timer.Start();
this.status = ProcessoStatus.OCIOSO;
}
public void processo()
{
Console.WriteLine(DateTime.Now);
}
}
Just out of curiosity, if you wear one
System.Threading.Timer
does not give the same effect? In this case it would already do in anotherthread
until.– Jéf Bueno
It comes to run a few times or hangs on the first?
– Leonel Sanches da Silva
Sets "stop executing". Runs once to the end and then no longer runs? Encrava (deadlock) on first run? On what line? If you remove
await Task.Run
the problem disappears? What makes the methodprocesso()
? This question needs more details.– dcastro
Every 1 second, the tick "writes" the date and time in a listview (in the example I put on the console to illustrate). If I leave the program open for a while (indeterminate) the time is not changing in listview. As to using Threading.Timer, I don’t know. I can search. I need the timer to run only when the "process" method is finished. That’s why I put a "start"/"stop" there. So it doesn’t overlap the executions.
– falrus