I have a BackgroundTask
which connects to a remote server and does some kind of action, download, upload, etc. This task runs every 15 minutes.
The UI
associated to the BackgroundTask
does the same and more.
However, the remote server allows only one session per login and I have only one login to that server. As such, I need a strategy to ensure that either one of these two are active and not both.
Currently, I store a value in LocalSettings
which indicates if the UI is open or not. If the UI is closed, the BackgroundTask
will do it's job as per normal. If the UI
is open, the BackgroundTask
will do nothing.
This works fine, except for when the BackgroundTask
is in the middle of it's Run and the UI is launched. I need a way of cancelling this BackgroundTask
immediately when the UI is launched, so that the UI can use the login to the remote server. Any tips or suggestions on how this may be achieved?
Thanks to Dave Smits for the solution.
In a nutshell:
Cancellation
event in Run
method.Cancellation
event handler ensure connected sessions are disconnected.OnLaunched
event is raised in the App
object, Unregister
the
task which raises the task Cancellation
event.