Imagine there are two buttons that call an asynchronous function
int packProcesses=0; //the number of processes we are dealing with
bool busy = false; //are we busy?
int v=10;
private async void button5_Click(object sender, EventArgs e)
{
packProcesses++;
busy = true;
Trace.WriteLine("PROCESSES " + packProcesses + " busy? " + busy);
//Do something
var result = await DelayAndReturnAsync(v);
//finished?
packProcesses--;
if (packProcesses <= 0) busy = false;
Trace.WriteLine("Processes " + packProcesses + " busy? " + busy);
}
private async void button6_Click(object sender, EventArgs e)
{
packProcesses++;
busy = true;
Trace.WriteLine("PROCESSES " + packProcesses + " busy? " + busy);
//Do something
var result = await DelayAndReturnAsync(v);
//finished?
packProcesses--;
if (packProcesses <= 0) busy = false;
Trace.WriteLine("Processes " + packProcesses + " busy? " + busy);
}
Where the asynchronous function is
async Task<int>DelayAndReturnAsync(int val)
{
await Task.Delay(TimeSpan.FromSeconds(val)).ConfigureAwait(false);
Trace.WriteLine("Time" + DateTime.Now);
return val;
}
and I want to have another button that calls both of the buttons. If I just put both click functions one after another I will have both processes started at once.
Since I want one processes to start after the other I do
private async void button8_Click(object sender, EventArgs e)
{
button5_Click(sender, e);
do
{
await Task.Delay(1000);
} while (busy);
button6_Click(sender, e);
}
I took the idea from this answer
Is this a good idea? I don't want to clog the CPU in order to do this. Is there a better way to wait for one process to complete to start the other?