0

We have a WPF app that also runs on a tablet. We are targeting Windows 10, .Net 4.6.2.

We have an async event handler that calls MessageBox.Show. The user clicks Yes and the app continues on doing some stuff.

When the app runs on tablet hardware, the app locks up for 10-20 seconds after the event completes. I cannot duplicate this from the desktop or in the simulator, only when it actually runs on the tablet.

I have isolated it to the MessageBox. When I take it out the app behaves normally. I feel like maybe it has something to do with threading.

Any ideas?

grinder22
  • 551
  • 4
  • 17

2 Answers2

2

using "async" will cause the MesssageBox.Show() method to be called on a separate thread. instead of putting the MesssageBox.Show() call in an async call consider putting it in a Thread, ensuring you declare it a background thread:

Thread messageBoxThread = new Thread(() => { yourMessageBoxCall(); );
messageBoxThread.IsBackground = true;
messageBoxThread.Start();
stuicidle
  • 297
  • 1
  • 8
  • This wasn't exactly my solution because it blocks the main thread. But you did lead me down a better research path. I solved it with a Dispatcher.BeginInvoke call. – grinder22 May 01 '17 at 15:38
2

Based on @stuicidle's clue, I went down a better research path. Many people have tried to solve the async MessageBox problem.

I ultimately got my solution from how can i use Messagebox.Show in async method on Windows Phone 8?

My code looks like this:

private async Task HandleEvent()
{
    var message = $"Continue?";
    MessageBoxResult result = MessageBoxResult.None;
    var dg = new Action(() => result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo));
    await Dispatcher.BeginInvoke(dg);

    if (result == MessageBoxResult.Yes)
        await DoSomeStuff();
}
Community
  • 1
  • 1
grinder22
  • 551
  • 4
  • 17