I'm practicing on async programming and tried to make a simple example to clarify my question. This is a demonstration so the aim is not change labels inside the same function but by using two separate functions namely func1 and func2.
So I want to start two async functions func1 and func2 almost at the same time with single click on Button1.
But as you see below in my code, I had to create another button called Button2 and Button2 click event to start func2 at the same with func1:
namespace AsyncSyncCompWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public async Task<bool> func1()
{
for (int i=0; i < 100; i++)
{
await Task.Delay(100);
labelLeft.Content = i.ToString();
}
return true;
}
public async Task<bool> func2()
{
for (int i = 0; i < 100; i++)
{
await Task.Delay(100);
labelRight.Content = i.ToString();
}
return true;
}
private async void Button1_Click(object sender, RoutedEventArgs e)
{
Button2.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
await func1();
}
private async void Button2_Click(object sender, RoutedEventArgs e)
{
await func2();
}
}
}
Now instead of creating the button Button2, can I create a method which would do exactly the same thing so I don't have to create an extra button? (Is a delegate needed for that?)
