Teratux
Teratux

Reputation: 23

Windows Phone 7 Async Method multiple invocations

I'm working on a Method which does some task asynchronously.

Let's call it: AsyncMethod();

The calling class subscribes to an event AsyncMethodCompleted which "sends" eventarguments with the result of the request back to the class which triggered the AsyncMethod() request.

Now my problem is that my AsyncMethod is sometimes called more than once to retrieve data, and as the time the AsyncMethod takes to complete can vary it's possible that my Subcribers receive data which they didn't request.

So my question is if there's a way to distinguish the results so that my subscriber classes only react to a result if the result matches their request.

Upvotes: 1

Views: 167

Answers (1)

SLaks
SLaks

Reputation: 887225

You should either use a separate class instance for each invocation, or replace the event with a callback passed to the method.

Or, better yet, use Task<T> instead.

Upvotes: 1

Related Questions