pinale
pinale

Reputation: 2214

keep an event waiting for another event completion - old shool

I need to do this but without async/await keywords (my app is compiled with .NET Framework 4.0)

TaskCompletionSource<bool> tcs = null;

private async void Button_Click(object sender, RoutedEventArgs e)
{
    tcs = new TaskCompletionSource<bool>();
    await tcs.Task;
    WelcomeTitle.Text = "Finished work";
}

private void Button_Click2(object sender, RoutedEventArgs e)
{
    tcs?.TrySetResult(true);
}

any idea?

Upvotes: 0

Views: 63

Answers (1)

Theodor Zoulias
Theodor Zoulias

Reputation: 43400

You need to manually attach a continuation to the task. This is the kind of burden that async-await was designed to alleviate.

TaskCompletionSource<bool> tcs = null;

private void Button_Click(object sender, RoutedEventArgs e)
{
    tcs = new TaskCompletionSource<bool>();
    tcs.Task.ContinueWith((_) =>
    {
        WelcomeTitle.Text = "Finished work";
    }, TaskContinuationOptions.ExecuteSynchronously);
}

private void Button_Click2(object sender, RoutedEventArgs e)
{
    tcs?.TrySetResult(true);
}

Upvotes: 1

Related Questions