Imagine there are two buttons that call an asynchronous function
int packProcesses=0; //the number of processes we are dealing with
bool busy = false; //
if you can at all, I would try and avoid having your button6_Click
and button5_Click
methods returning void
. if instead you have them return a Task
you can await
them.
private async Task button5_Click(object sender, EventArgs e)
{ ... }
private async Task button8_Click(object sender, EventArgs e)
{
await button5_Click(sender, e);
await button6_Click(sender, e);
}
edit:
private async Task HandleButton5_Click()
{
...
}
private async void button5_Click(object sender, EventArgs e)
{
await HandleButton5_Click();
}
private async void button8_Click(object sender, EventArgs e)
{
button5_Click(sender, e);
button6_Click(sender, e);
}
You can move your logic from inside the handler to another method:
private async void button1_Click(object sender, EventArgs e)
{
await Process1();
}
private async Task Process1()
{
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 button2_Click(object sender, EventArgs e)
{
await Process2();
}
private async Task Process2()
{
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);
}
Then you can await both them:
private async void button3_Click(object sender, EventArgs e)
{
await Process1();
await Process2();
}