0

My question is fairly simple, but I suspect the answer won't be. In my WP7 app, I am calling a REST web service to GET some data which I deserialize into class objects.

My request method and its AsyncCallBack method live inside a class (an MVVM ViewModel), and are invoked from inside an instance method on the class (LoadData).

The AsyncCallBack deserializes the json retrieved from the web service into an object. I need to add this object to a collection on the class where all of this is taking place - like so:

this.Collection1.Add(retrievedObject); 

Of course, since AsyncCallBack is static, I can't access the "this" keyword. I also can't return the retrievedObject to the caller, because the AsyncCallBack has to return void. I realize I'm probably the victim of some basic misunderstanding here. How do I solve this?

Thanks!

Andrew B Schultz
  • 1,512
  • 1
  • 23
  • 41
  • Why does your `AsyncCallback` have to be static? – Lee Sep 01 '12 at 14:36
  • It doesn't; but I've noticed that it always is in examples. I'm worried that making it non-static will make it so it's not threadsafe or something like that. As I said, I'm not super familiar with these methods ... but if it's not a concern, then please let me know, as that would answer my question! By simply making the AsyncCallBack an instance method and doing this.Collection1.Add(retrievedObject) inside the AsyncCallBack, I get an "Invalid cross-thread access" exception. – Andrew B Schultz Sep 01 '12 at 14:37
  • 1
    Your callback will probably be invoked on a non-UI thread, so you may need to synchronise access to your collection (or use a thread-safe collection), but there's no requirement for your callback to be a static method. – Lee Sep 01 '12 at 14:41
  • You can't access any controls from any other thread than the main thread (i.e. the thread that created the controls). You need to use `Dispatcher` to run that code in the main thread. See: http://stackoverflow.com/questions/6238064/windows-phone-7-event-invoke – Guffa Sep 01 '12 at 14:47
  • thank you guys! Once you pointed out the main concerns, I found this great solution in the answer from @SteveWillcock: http://stackoverflow.com/questions/3420282/unauthorizedaccessexception-invalid-cross-thread-access-in-silverlight-applicat – Andrew B Schultz Sep 01 '12 at 15:05

2 Answers2

3

Does it have to be static? No.

The callback doesn't have to be static, but you are right to be concerned about thread safety. The callback method will be called on a different thread, so if it uses some data that the main thread is also using, you have to synchronise the access to that data.

Does it have to return void? Yes.

The callback method can't return anything to the method that has started the asynchronous task, because that method returns before the task is completed. For the callback method to set that return value, it would have to travel back in time.

Community
  • 1
  • 1
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Awesome - thanks! Here's (what seems to me like) a great method for handling the threading: Steve Willcock's answer on this thread: http://stackoverflow.com/questions/3420282/unauthorizedaccessexception-invalid-cross-thread-access-in-silverlight-applicat – Andrew B Schultz Sep 01 '12 at 15:06
1

Read how to use the Asynchronous Programming Pattern (APM): msdn.microsoft.com/en-us/library/ms228963.aspx

Paul Michalik
  • 4,331
  • 16
  • 18