In my application I have several "subprocesses". All of them issue information in which they are displayed on the Form.
I used System.Windows.Forms.Timer
:
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();
await Task.Run(() => this.processo());
this.timer.Start();
this.status = ProcessoStatus.OCIOSO;
}
}
One of these subprocesses checks a webservice for updated data. But for this I need all other processes to stop.
// main thread
Class Y
{
public void a()
{
X obj1 = new X();
X obj2 = new X();
obj1.f();
obj2.f();
}
public sync()
{
obj1.timer.Enabled = false;
while (obj1.status != ProcessoStatus.OCIOSO)
{
// faz nada no loop, apenas aguarda o método tick terminar
}
obj2.timer.Enabled = false;
while (obj2.status != ProcessoStatus.OCIOSO)
{ }
// pega os dados novos do webservice
}
}
The problem is that the while, in many cases, hangs the main thread, causing the status to never be "idle."
Any suggestions for improving this?