0

I have a loading method which runs asynchronously. Inside this method, I change the Checked-State of a RadioButton.

This works fine and it doesn't matter if I execute it directly or via Dispatcher.BeginInvoke.

However, the RadioButton has a "CheckedChanged"-Event. When I call

Dispatcher.Invole((Action)()=>{rb.IsChecked=true;}));

I would expect my asynchronous method to wait for the checked changed event (which runs an async method again) being done.

In the attached code you will see that the execution-order is:

  1. "BeginInvoke"
  2. "EndInvoke"
  3. "RbChanged"

What I would like to have is:

  1. "BeginInvoke"
  2. "RbChanged"
  3. "EndInvoke"

Any options for this?

MainWindow.xaml.cs

 public MainWindow()
        {
            InitializeComponent();
        }

        private async enter code herevoid Window_Loaded(object sender, RoutedEventArgs e)
        {
            await RightAfterLoading();
        }


        private async Task RightAfterLoading()
        {
            Debug.WriteLine("BeginInvoke");
            await Dispatcher.BeginInvoke((Action)(() => { rb.IsChecked = true; }));
            Debug.WriteLine("EndInvoke");
        }

        private async void rb_Checked(object sender, RoutedEventArgs e)
        {
            await SomethingAfterChecked();
        }

        private async Task SomethingAfterChecked()
        {
            string abc = string.Empty;
            await Task.Run(() => abc = "RbCheckedChanged");
            Console.WriteLine(abc);
        }

And MainWindow.xaml looks like:

<Window x:Class="AsyncAwaitIssue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:AsyncAwaitIssue"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="525"
        Height="350"
        Loaded="Window_Loaded"
        mc:Ignorable="d">
    <Grid>
        <RadioButton x:Name="rb"
                     Checked="rb_Checked"
                     Content="CheckedOrNot"
                     IsChecked="False" />
    </Grid>
</Window>

1 Answer 1

0

This is the very nature of async void methods. When you set IsChecked it will wait for the event handler for the changed event to execute, but when you execute that handler all you ever do is start an asynchronous operation. The handler will complete before those asynchronous operations have finished.

Ideally you'd avoid such a situation. If you have something that needs to happen after the work you're doing in the changed event, it'd be better if that change event would perform that operation. If you can't, you'd need to manually create some form of communication between these sections of code and couple these operations together fairly tightly.

2
  • Thanks for your reply Servy.
    – nicn
    Oct 14, 2016 at 5:47
  • I was expecting an answer like this, but I wanted to see if there was a solution for this. However, we will need to find another way to work around this "issue".
    – nicn
    Oct 14, 2016 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.