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:
- "BeginInvoke"
- "EndInvoke"
- "RbChanged"
What I would like to have is:
- "BeginInvoke"
- "RbChanged"
- "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>