Waiting for results on ToCollectionAsync() or InsertAsync()

Category: azure mobile


Tino Donderwinkel on Tue, 28 Jan 2014 22:04:33

I have to implement an interface, and within the implementation I want to work with Azure Mobile Services.

Since this is an interface implementation, I cannot use the async modifier; I have to wait for the results.
When I wait for the result using a Task and a Wait statement, the Wait waits indefinitely.

Task task = todoTable.InsertAsync(todoItem);
task.Wait(); // This hangs indefinitely.

Any thoughts on how I can 'wait' for the results to come back?




CarlosFigueira on Tue, 28 Jan 2014 22:41:29

You really can't wait - that would freeze the application UI, which is why the method is defined to return a Task. The result of the method is marshalled to the UI thread; if you start the call on the UI thread, then Wait on the task, you'll have introduced a deadlock. You should consider changing the interface to define an asynchronous operation (i.e., one which returns a Task<T>) instead of trying to force an asynchronous operation to be executed synchronously.

Rafael Serna on Tue, 28 Jan 2014 23:17:15


The answer of Carlos Figueira, is completely correct. You can implement an asynchronous method for do that.

public async void InsertItem()
    await todoTable.InsertAsync(todoItem);

Tino Donderwinkel on Wed, 29 Jan 2014 22:08:36

Unfortunately, the interface implementation I'm working on is defined by a third party. There is no way that I can have the signature modified.

I've tried some different methods, but I have reverted back to using a plain HttpWebRequest against the REST API of Azure Mobile Services. This interface is fairly easy to implement, specifically with the latest version of the .NET Framework that come with JSON (de)serializers out of the box.

Thanks all for the help! (Carlos; kudos will arrive...)