Wait for async task on application exit from UI thread
<p>I'm writing an UI app. If some exception happens I need to do some work before application exit. 
So I subscribed to <code>AppDomain.CurrentDomain.UnhandledException</code> event.</p>

<pre><code>AppDomain.CurrentDomain.UnhandledException += HandleException;
</code></pre>

<p><code>HandleException</code> method performs async saving to remote(because there are no sync api).</p>

<pre><code>private void HandleException(object sender, UnhandledExceptionEventArgs args)
{
    foreach (var user in UsersCollection.ToArray())
    {
        try
        {
            foreach (var session in user.Sessions.ToArray())
            {
                try
                {
                    SaveSessionAsync(session).Wait();
                }
                catch (Exception e)
                {
                    Logger.Error("Can't save session: " + session, e);
                }
            }
        }
        catch (Exception e)
        {
            Logger.Error("Can't save sessions of user " + user, e);
        }
    }
}
</code></pre>

<p>Before application exit I need to be sure that I saved all sessions(tried, at least). But if I put <code>Wait()</code> there I get a deadlock and application never stops. 
As far as I know, <code>await</code> may help me in normal situation(when I'm in UI thread but not in app termination state), but <code>await</code> does not awaits on application exit. So my saving tasks may be aborted. But I need them to finish.</p>

<p>Is there a way of waiting for guaranteed finish of <code>SaveSessionAsync</code> task without creating a deadlock?</p>

<p>BTW: SaveSessionAsync has Parse.com API inside</p>