1

My app hangs and doesn't respond after I put my machine to sleep and then wake it again.

Here's my repro code:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        App.Current.Suspending += OnSuspending;
        App.Current.Resuming += OnResuming;
    }

    private void OnSuspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        var syncObject = new Object();
        var dispatcher = this.Dispatcher;

        lock(syncObject)
        {
            Task.Run(() =>
            {
                lock (syncObject)
                {
                    dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () =>
                        {
                            new MessageDialog("Oh no! Deadlock during suspension!").ShowAsync();
                        }).AsTask().Wait();
                    deferral.Complete();
                }
            }).Wait();
        }
    }

    private async void OnResuming(object sender, object e)
    {
        await new MessageDialog("Hello, I resumed").ShowAsync();
    }
}

What's more - if I use VS's lifecycle events buttons to force suspend/resume - VS also hangs.

Romasz
  • 29,662
  • 13
  • 79
  • 154
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100

1 Answers1

1

Oh, I have a deadlock in my suspension code. If you have a deadlock in your suspension code - the Resume event never fires and the app hangs. It also seems like VS doesn't respond to it very well.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • Moreover - because of this [Extended execution doesn't seem to work well](http://stackoverflow.com/q/36105996/2681948). Generally I think we cannot assume that it will always behave like that, the docs says that code in suspension can last limited time - not sure if OS won't terminate it one day. – Romasz Mar 21 '17 at 17:02