HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Get result from UI thread to non-UI thread when using await Task.Run

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
resultnonawaitgetthreadusingwhenfromtaskrun

Problem

I have an application where I use Task to run an operation. During the operation, there can be a problem which will need some user interaction (continue or not). I need to bubble up a messagebox and get the user result back to the operation thread.

This is how I got it working:

public interface IPlugin
{
    void ConnectAndProduce(Func retryRequest);
}

public partial class MainWindow : MetroWindow
{
    [ImportMany(typeof(IPlugin))]
    private IEnumerable Plugins;

    public MainWindow()
    {
        InitializeComponent();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        Func func = message =>
        {
            var result = Application.Current.Dispatcher.Invoke(new Func>(async () =>
                {
                    var mySettings = new MetroDialogSettings()
                    {
                        AffirmativeButtonText = "YES",
                        NegativeButtonText = "NO",
                        ColorScheme = MetroDialogColorScheme.Accented
                    };

                    return await this.ShowMessageAsync("Hello!", message, MessageDialogStyle.AffirmativeAndNegative, mySettings);
                }));

            if ((result as Task).Result != MessageDialogResult.Negative)
                return true;

            return false;
        };

        ImportPlugins();
        var plugin = Plugins.FirstOrDefault();

        await Task.Run(() =>
        {
            plugin.ConnectAndProduce(func);
        });
    }

    public void ImportPlugins()
    {
        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
        CompositionContainer container = new CompositionContainer(catalog);
        container.ComposeParts(this);
    }
}


And here is the Plugin what is going to do all the work:

```
[Export(typeof(IPlugin))]
public class Machine : IPlugin
{
public void ConnectAnd

Solution

You're scheduling a new non-UI operation that immediately goes back and schedules an operation to run in the UI thread, which then does some work.

Just avoid all of that, and do the work that you want to do right from the start, since you're in the UI thread:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var mySettings = new MetroDialogSettings()
    {
        AffirmativeButtonText = "YES",
        NegativeButtonText = "NO",
        ColorScheme = MetroDialogColorScheme.Accented
    };

    var response = await this.ShowMessageAsync("Hello!", "message", MessageDialogStyle.AffirmativeAndNegative, mySettings);

    if(response == MessageDialogResult.Negative)
        return;
}

Code Snippets

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var mySettings = new MetroDialogSettings()
    {
        AffirmativeButtonText = "YES",
        NegativeButtonText = "NO",
        ColorScheme = MetroDialogColorScheme.Accented
    };

    var response = await this.ShowMessageAsync("Hello!", "message", MessageDialogStyle.AffirmativeAndNegative, mySettings);

    if(response == MessageDialogResult.Negative)
        return;
}

Context

StackExchange Code Review Q#80074, answer score: 7

Revisions (0)

No revisions yet.