During last days I've been developing some Windows 8 HTML5/WinJS application. This application does some job using a WinRT component.
The JavaScript part starts an asynchronous operation in collaboration with the whole WinRT component: the callback function is given by JavaScript and the WinRT calls it when it has some asynchronous result.
I forgot to mention that the whole Windows 8 application isn't developed using the single page approach.
If the whole asynchronous operation is called in some page and you don't navigate to other pages, everything works as expected.
But, what happens when you navigate to other page? When the WinRT component needs to notify the JavaScript part about the result of the asynchronous operation: ACCESS DENIED EXCEPTION! And your application crashes.
IAsyncOperation<T>
: before navigating to other page, I call the .cancel()
method in JavaScript => NO LUCK
WinJS.Application.sessionState
in order to be sure that the whole function isn't destroyed by the garbage collector => NO LUCK
Do I have any chance to notify the WinRT component to cancel it's asynhcronous operation and don't try to return the control to the JavaScript callback?
Thanks in advance
You can check that others have found the same problem before:
I've found a solution after some trial-errors.
Answering to my own question:
Do I have any chance to notify the WinRT component to cancel it's asynhcronous operation and don't try to return the control to the JavaScript callback?
Yes, but not directly.
So, how?
CancellationTokenSource
property in the C# WinRT component and, assign it just before the call to the asynchronous operation.CancellationTokenSource
and set it to the property created in the previous step just before calling the asynchronous operation.ContinueWith
method the asynchronous operation before converting the .NET Task into a WinRT IAsyncOperation<T>
and give the CancellationToken
created as part of the previously instantiated CancellationTokenSource
to the ContinueWith
method (see cancelling tasks: http://msdn.microsoft.com/en-us/library/dd997396.aspx).CancelCurrentAsyncOperation
method in the WinRT component that will call CancellationTokenSource.Cancel()
.CancelCurrentAsyncOperation
method in WinJS/JavaScript before navigating to another page.public sealed class MyWinRTComponent
{
private CancellationTokenSource { get; set; }
public void CancelLastAsyncOperation()
{
if(CancellationTokenSource != null)
{
CancellationTokenSource.Cancel();
}
}
public IAsyncOperation<string> DoSomethingAsync()
{
CancellationTokenSource = new CancellationTokenSource();
return DoSomethingAsync()
.ContinueWith<string>(task => task.Result, CancellationTokenSource);
}
}
var component = new MyWinRTComponent();
component.doSomethingAsync().then(function(text) {
// Do stuff
});
// Before navigating to other page
component.cancelCurrentAsyncOperation();
That worked for me!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With