Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP App always exits with non-zero return code

Tags:

c#

destructor

uwp

To my understanding, an application that doesn't finish and return with a 0 error code didn't terminate correctly. Somehow though, any UWP App I make, even the very default one, finishes with error code 1 every single time. Moreover, it's main class's Dispose() and destructor are never called. Is this expected behaviour?

To reproduce, I just made the default UWP app:

  • Right click on Solution -> Add -> New Project...
  • Then under Other Languages -> Visual C# -> Windows Universal -> "Blank App (Universal Windows)"
  • Then Build and Deploy the app.

If you now run and then close the app, it seems like it's being force-quit. The last output I get is:

The thread 0x35a4 has exited with code 0 (0x0).
The thread 0x34f4 has exited with code 1 (0x1).
The program '[3376] App1.exe' has exited with code 1 (0x1).

So I take this to mean that a thread fails, and then that fails the application. Is this correct, and is this bad?

Moreover, I added the following code to the main App.xaml.cs:

    ~App()
    {
        System.Diagnostics.Debug.WriteLine("Calling destructor");
    }

    // (Implement Disposable interface)
    public void Dispose() 
    {
        System.Diagnostics.Debug.WriteLine("Calling dispose");
    }

Neither is ever called. What's going on?

like image 227
Yellow Avatar asked Oct 17 '22 05:10

Yellow


1 Answers

I would like to suggest you to play with UWP lifecycles and events. As far as my understanding goes, The application when closed by the user go to the suspended mode and then it is terminated. The UWP application lifecycle goes through a series of steps wherein it even enters and leaves the background while at start or suspend. In App.Xaml.Cs you can go ahead any play with the events and understand how the application lifecycle works.

like image 166
Apoorv Avatar answered Oct 21 '22 04:10

Apoorv