locked
Is there a CoreDispatcher for background tasks? RRS feed

  • Question

  • My component fires events which works fine from within an app. However, from a background task I cannot call CoreWindow::GetForCurrentThread()->Dispatcher to get the dispatcher. How would I communicate with the background task then?
    Tuesday, April 16, 2013 10:09 AM

Answers

  • Background Tasks are supposed to provide functionality even when your app is not running or is suspended which is why you don't have access to the UI. If you want to share data between the Background Task and your app, the only options you have is to use the ApplicationData class or save data to some file.

    @prashantphadke || Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog! http://aka.ms/t4vuvz

    Wednesday, April 17, 2013 8:37 PM
    Moderator

All replies

  • You can use the ApplicationData class to store data in the application's store, for example ApplicationData.Current.LocalSettings and share data between the Background task and the app.

    Thanks,

    Prashant.


    @prashantphadke || Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog! http://aka.ms/t4vuvz

    Tuesday, April 16, 2013 11:53 PM
    Moderator
  • This is not an option in my scenario. The background task opens a database and interacts with it and needs to perform async operations with the database.
    Wednesday, April 17, 2013 9:51 AM
  • Background Tasks are supposed to provide functionality even when your app is not running or is suspended which is why you don't have access to the UI. If you want to share data between the Background Task and your app, the only options you have is to use the ApplicationData class or save data to some file.

    @prashantphadke || Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog! http://aka.ms/t4vuvz

    Wednesday, April 17, 2013 8:37 PM
    Moderator
  • This is not what this question is about at all, sorry. Let me rephrase: I can call WinRT Async API from a background task. How does WinRT handles event notification in such scenarios? ie I create a FolderQuery in a background task and attach a "contentchanged" event handler. How is the WinRT component calling my event handler without a dispatcher?
    Friday, April 19, 2013 4:31 AM
  • The system uses asynchronous RPC to perform these type of operations.

    Thanks,

    Prashant.


    @prashantphadke || Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, Windows Store & Phone Developer Solutions

    Saturday, April 20, 2013 12:57 AM
    Moderator
  • Not sure what you mean. How would look that in practice?
    Thursday, April 25, 2013 11:26 AM
  • This question has been answered properly:

    Background Tasks are supposed to provide functionality even when your app is not running or is suspended which is why you don't have access to the UI. If you want to share data between the Background Task and your app, the only options you have is to use the ApplicationData class or save data to some file.

    You don't have access to asynchronous RPC because it's not part of the API. 


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 29, 2013 6:53 PM
    Moderator
  • Despite what you write here, calling my own Component from a background task and communicating with the background task does indeed work. I am sorry, the question has not been properly answered. I was not talking about having a UI or communication with some form of UI from a background task. Maybe it was not properly described what I want to do:

    background.js

    var db = SQLite3JS.openAsync("theappsdatabase")
    .then (function(db) {
      db.oninsert = function(event) {
        // perform additional information
      };
      db.allAsync("SELECT * from unfinishedWork")
      .then(function(rows) {
        db.runAsync("INSERT something into some_table..");
      })
    });

    The problem has been calling the oninsert delegate from the component. The code in the C++ component used the CoreDispatcher to call the event handlers. But since in background tasks there is no Dispatcher required, its simply a matter of directly calling the event handler delegate:

    if (dispatcher == nullptr) {
                _Insert(this, event);
                return;
              } else {
                handler = ref new DispatchedHandler([this, event]() {
                  _Insert(this, event);
                });
              }

    So, that way it works :) Question is: is that the correct way to do it?

    Monday, May 13, 2013 4:46 PM