Access to UI thread during application pause?

I have a UWP application that allows users to create and modify text documents. It’s not easy for me to get a save mechanism to work with the application pause / resume life cycle.

Here is what I have:

  • All disk access is done in a separate background thread to support the user interface.
  • This background thread also serializes all disk access to ensure that only one read or write operation is performed at any time.
  • Before and after saving the document, the user interface is updated to indicate that the save is in progress. This is scheduled in the user interface thread throughDispatcher.RunAsync()

When the application pauses:

  • When pausing, I need to save the document for the last time to make sure that all changes are on the disk (the application can be completed during the pause)
  • Therefore I request ExtendedExecutionSession
  • I plan on one last save operation in my queue
  • Then I wait in line to handle all pending disk access operations.
  • Finally, I mark the extended execution session as completed

My problem:

  • An already scheduled save operation completes disk access, and then tries to update the user interface in the main stream through Dispatcher.RunAsync(). The background queue waits for this task to complete, but it never does, because by this time the UI thread has already stopped .

→ , , .

:

Application sequence diagram

, :

  • ? " "? Dispatcher.RunAsync(), ""?
  • , , , ?
  • , ?
  • ?

:

, , , , , , , , .

+6
1

!

: , , , Dispatcher. , :

public async Task SaveAsync()
{
   // .. save to disk ..

   await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
   {
      // update UI here
   }

   // Save Complete!
}

, , , - , Dispatcher, , //Save Complete!, , Dispatcher .

, , , . :

public event EventHandler SaveStarted;
public event EventHandler SaveCompleted;

public async Task SaveAsync()
{
   SaveStarted?.Invoke(this, EventArgs.Empty);

   // .. save to disk ..

   SaveCompleted?.Invoke(this, EventArgs.Empty);
}

/ , SaveAsync(), .

Unloaded, , . .


1

, Dispatcher:

public void OnSaveCompleted(object sender, EventArgs e)
{
   Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
   {
      // update UI here...
   }
}

void, .


2

, . , . .Net Standard, . - , , .

UWP, , , XAML, Dispatcher , .

, , . hasChanges , SaveAsync(). - // /etc , , , , .

UWP - , . - , , .

+4

All Articles