Windows Store app Development Snack: Being a sharing target while your app is running

Submitted by Robert MacLean on Thu, 09/20/2012 - 15:55

For more posts in this series, see the series index.

imageBeing a share target seems like a great idea to get people to use your application more, however it does have a fairly complex problem: if I do a share to my application, while my application is running what happens – does it start a new instance or use the existing instance. You may say this can’t happen since Windows 8 doesn’t allow more than one application to run at a time… but you would be wrong.

Snap view in Windows 8 allows for two Metro style applications to run side by side thus allowing two apps to run at the same time. In fact you can run three apps at a time: one snapped, one filled and then you do a share target which launches a third app!

So back to the question, what happens if you app is say running in snapped view and you do a share from the filled app to your app which is currently snapped? The answer is it uses your existing application but from a separate thread.

To test this I put a simple Boolean field into the constructor of my App class which I set to true, then when the OnShareTargetActivated event was raised I checked the value of that Boolean field, and it was true if the app was running!

You should come up with a solution for this (or at least test it) – in my case the OnShareTargetActivated wrote to the application store and then my main UI which used that would poll for changes. I had to do this rather than triggering the UI because the OnShareTargetActivated was launched in a separate thread and trying to trigger caused a cross thread issue (I did try dispatcher fixes but that lead to a variety of COM issues).