locked
[UWP]Background Transfer or Service Help RRS feed

  • Question

  • Hi,

    I want to add Background Task in my UW App for syncing database.

    I need this this task to

    * Run only after user closes Application

    * But it shouldn't start every time App gets suspended.

    Is there any way I can start it after App termination or something? Can anybody give me right direction for this?

    I appreciate if somebody gives me Tutorial or sample link


    xSHERU


    Tuesday, July 26, 2016 2:28 AM

Answers

  • Hi xSHERU,

    Welcome to the Developing Universal Windows apps forum!

    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools 

    I have added tag for you this time, and you would need to add tag to title by yourself next time.

    >>” 1. Can BackgroundTask access Main Application Local Folder to access main database? If not what can I do?”

    Yes.

    >>” 2. Can BackgroundTask access Main Application LocalSettings? If not is there any good way to transfer updated settings to Main Application?”

    Yes.

    >>” I am asking this because I cant reference Main App in BackgroundTask Project or BackgroundTask in Main Project at same time. It shows Cyclic Dependancy Error.”

    You don’t need to add your main app project reference in your background task project. When you have added your background task project reference in your main app project, then in background task, you could access the localfolder, localsettings directly.

    According to your above three questions, it seems that you’re not familiar with background task in UWP. Please refer to Azat Tazayan’s suggestion, read the Guidelines for background tasks. After that, you would know how to use background task in UWP.

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Xavier Xie-MSFT Wednesday, July 27, 2016 6:28 AM
    • Marked as answer by xSHERU Thursday, July 28, 2016 3:08 AM
    Wednesday, July 27, 2016 6:26 AM
  • You forgot call GetDeferral on taskinstance  

    change your code as below

            public async void Run(IBackgroundTaskInstance taskInstance)
            {
                var deferral = taskInstance.GetDeferral();
                await SyncData();
                deferral.Complete();
            }
    
            public async Task SyncData()
            {
                LiveOperationResult GetCloudDatabaseInfo = await ConnectClient.GetAsync(FileID);
                StorageFile BackupFile = await LocalFolder.GetFileAsync("Database.db");
                LiveOperationResult BackupOPResult = await ConnectClient.BackgroundUploadAsync(FolderID, BackupFile.Name, BackupFile, OverwriteOption.Overwrite);
            }



    • Edited by Azat Tazayan Wednesday, July 27, 2016 11:11 PM
    • Marked as answer by xSHERU Thursday, July 28, 2016 3:07 AM
    Wednesday, July 27, 2016 11:03 PM

All replies

  • Tuesday, July 26, 2016 8:06 AM
  • Hi Azat,

    Thanks for quick reply.

    I want to clear some things...

    1. Can BackgroundTask access Main Application Local Folder to access main database? If not what can I do?

    2. Can BackgroundTask access Main Application LocalSettings? If not is there any good way to transfer updated settings to Main Application?

    I am asking this because I cant reference Main App in BackgroundTask Project or BackgroundTask in Main Project at same time. It shows Cyclic Dependancy Error.

    3. If I create BackgroundTask (Windows Runtime Component) do I have to register this new project for API access as well or it uses Main App API Acess? Like Microsoft One Drive, Google Login etc.


    xSHERU




    • Edited by xSHERU Tuesday, July 26, 2016 11:05 AM
    Tuesday, July 26, 2016 10:42 AM
  • Hi xSHERU,

    Welcome to the Developing Universal Windows apps forum!

    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools 

    I have added tag for you this time, and you would need to add tag to title by yourself next time.

    >>” 1. Can BackgroundTask access Main Application Local Folder to access main database? If not what can I do?”

    Yes.

    >>” 2. Can BackgroundTask access Main Application LocalSettings? If not is there any good way to transfer updated settings to Main Application?”

    Yes.

    >>” I am asking this because I cant reference Main App in BackgroundTask Project or BackgroundTask in Main Project at same time. It shows Cyclic Dependancy Error.”

    You don’t need to add your main app project reference in your background task project. When you have added your background task project reference in your main app project, then in background task, you could access the localfolder, localsettings directly.

    According to your above three questions, it seems that you’re not familiar with background task in UWP. Please refer to Azat Tazayan’s suggestion, read the Guidelines for background tasks. After that, you would know how to use background task in UWP.

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Xavier Xie-MSFT Wednesday, July 27, 2016 6:28 AM
    • Marked as answer by xSHERU Thursday, July 28, 2016 3:08 AM
    Wednesday, July 27, 2016 6:26 AM
  • Hi Xavier,

    I have never used Background Task before but after going through the link now I have pretty good idea of it. Thanks.

    But I am facing some issues will you please help me with them.

    After firing up Background Task I am using this code, its not working. I tried debugging it but pointer just stops at the same line for minutes it doesn't give me any exception. It just stays at first line and if I remove first line it stays on second and same till the end...

            public void Run(IBackgroundTaskInstance taskInstance)
            {
                SyncData();
            }
    
            public async void SyncData()
            {
                LiveOperationResult GetCloudDatabaseInfo = await ConnectClient.GetAsync(FileID);
                StorageFile BackupFile = await LocalFolder.GetFileAsync("Database.db");
                LiveOperationResult BackupOPResult = await ConnectClient.BackgroundUploadAsync(FolderID, BackupFile.Name, BackupFile, OverwriteOption.Overwrite);
            }


    xSHERU

    Wednesday, July 27, 2016 10:19 PM
  • You forgot call GetDeferral on taskinstance  

    change your code as below

            public async void Run(IBackgroundTaskInstance taskInstance)
            {
                var deferral = taskInstance.GetDeferral();
                await SyncData();
                deferral.Complete();
            }
    
            public async Task SyncData()
            {
                LiveOperationResult GetCloudDatabaseInfo = await ConnectClient.GetAsync(FileID);
                StorageFile BackupFile = await LocalFolder.GetFileAsync("Database.db");
                LiveOperationResult BackupOPResult = await ConnectClient.BackgroundUploadAsync(FolderID, BackupFile.Name, BackupFile, OverwriteOption.Overwrite);
            }



    • Edited by Azat Tazayan Wednesday, July 27, 2016 11:11 PM
    • Marked as answer by xSHERU Thursday, July 28, 2016 3:07 AM
    Wednesday, July 27, 2016 11:03 PM
  • Hi Azat,

    Nice to hear from you again... Let me try it


    xSHERU

    Wednesday, July 27, 2016 11:08 PM
  • Hi Azat,

    Everything works great now...

    But I couldnt figure out about trigger. I want to run background task run after app closes and only once...


    xSHERU

    Thursday, July 28, 2016 12:32 AM