UWP App crashes when main process tries to get a folder while background process also uses the same folder

Andreas Löwer 96 Reputation points
2021-09-20T07:04:20.027+00:00

UWP App starts a Background Process. This process communicates with a Rest service and writes the result to files in a subfolder of ApplicationData.Current.Localfolder

Main Process tries to open that folder to check for new Files. When this happens, while the background process is still accessing the folder, the App crashes without an Exception that can be caught. If I wait for the Background Process to drop Access to the folder, everything works as expected.

I've added a global Exception Handler in App.xaml.cs constructor, but even there the exception is not caught.

How can i handle this error?

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Andreas Löwer 96 Reputation points
    2021-09-22T09:28:14.04+00:00

    If anyone else encounters this problem, here is the solution that finally worked for me:

    • Add a PublisherCacheFolders extension to App Manifest
    • access the shared folder from both processes via Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder
    • put Files directly into this folder, don't create subfolders.

    This way, concurrent access is possible for both processes. If any of the processes creates a directory, this directory seems to be locked to the creating process.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Roy Li - MSFT 33,356 Reputation points Microsoft Vendor
    2021-09-21T02:00:02.607+00:00

    Hello,

    Welcome to Microsoft Q&A!

    For your scenario, I'd suggest you handle the BackgroundTaskRegistration.Completed Event to check whether the background task is completed. Make an alert to tell the users the process is not finished when the background task is trigger. Then enable the check button when the event is fired because the background task is finished. This should be able to avoid your issue.

    The code looks like this:

    task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);  
      
      
    private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)  
    {  
        UpdateUI();  
    }  
    

    You could see the code sample here: GitHub background task sample. You could also check the document here: Handle background task completion using event handlers

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Andreas Löwer 96 Reputation points
    2021-09-21T05:06:06.017+00:00

    The Problem is, that the Background Process is started once when opening the app and runs in the Background forever. So the completed event doesn't help.
    But maybe I could use the LocalSettings to share a flag, if the background process is running currently.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.