troubleshooting Question

Get back to UI thread from ThreadPool thread?

Avatar of deleyd
deleydFlag for United States of America asked on
C#* WPF
8 Comments1 Solution28 ViewsLast Modified:
I have a very simple sample project which displays the problem I'm having in a much larger project. Here's the entire code:
It has a MainWindow.xaml with a single button:
<Window x:Class="WpfApp7TestAsyncUI.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp7TestAsyncUI"
        mc:Ignorable="d"
        Title="MainWindow" Height="250" Width="400">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="160,72,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

    </Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;

namespace WpfApp7TestAsyncUI
{
    public partial class MainWindow : Window
    {
        private MainWindowViewModel viewModel;
        public MainWindow()
        {
            this.viewModel = new MainWindowViewModel();
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.viewModel.HandleButtonClick();
        }
    }
}
MainWindowViewModel.cs
using System.Threading.Tasks;

namespace WpfApp7TestAsyncUI
{
    public class MainWindowViewModel
    {
        public async void HandleButtonClick()
        {
            await Task.Delay(100).ConfigureAwait(false);
            Window1 window1 = new Window1();
        }
    }
}
There's also a simple Window1.xaml which is empty:
<Window x:Class="WpfApp7TestAsyncUI.Window1"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp7TestAsyncUI"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        
    </Grid>
</Window>
Window1.xaml.cs
using System.Windows;

namespace WpfApp7TestAsyncUI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}
That's it. The problem is in MainWindowViewModel.HandleButtonClick()
        public async void HandleButtonClick()
        {
            await Task.Delay(100).ConfigureAwait(false);
            Window1 window1 = new Window1();
        }
Here the await Task.Delay(100).ConfigureAwait(false); causes the next line to resume on a ThreadPool thread. From that ThreadPool thread, I need a way to get back to the UI thread so I can create and display a window. So far I haven't found a way.

There is no Dispatcher so I can't do Dispatcher.Invoke().

The sample code here currently bombs complaining that I'm not on a STA thread.

I'm trying to rescue a large project here I inherited (they always give me the Legacy code that doesn't work anymore) and this is basically the problem it's currently having. I'm wondering how I can rescue it by gradually refactoring it, rather than doing a major overhaul. Is there a way to get back to the UI thread from this spot where I'm trying to create Window1?

(Oh yes, I can't take the simple "Remove the .ConfigureAwait(false);" If I ran everything on the UI thread yes it would work but the UI wouldn't be responsive. The problem is how to display a window with a message that asks for User Input when that code is buried deep in code that's running on a background or ThreadPool thread. -- Without doing a major rewrite. [Would like to slowly migrate towards how it should be, rather than one risky major rewrite step.])
ASKER CERTIFIED SOLUTION
deleyd
Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros