I’m running an asynchronous web request on Windows Phone 7 and I want the user to be able to cancel the request when it takes too long. I show a message box by calling:
IAsyncResult result = Guide.BeginShowMessageBox("Synchronize", "Synchronizing with MyWebsite.com...", new string[] { "Cancel" }, 0, MessageBoxIcon.None, OnEndDialog, null);
In the OnEndDialog callback I can cancel the request, which works fine.
But when the request is finished I want to close the message box from my code. Unfortunately, when I call
Guide.EndShowMessageBox(result);
from my code (yes, I use dispatcher.BeginInvoke to run the call on the UI thread), it turns out to be a blocking call and the dialog is still waiting for the user to press the Cancel button.
Is there any way to close the message box from code?
I guess it is not possible to close the Guide message box from code.
I solved the problem by using the MessagePrompt control from the Coding4Fun Windows Phone Toolkit. You can call OnCompleted(…) to close the message box.
I added a Dismiss method to the base class (UserPrompt) which can be called from a background thread: