how to make asynchronous service call to synchronous in Windows Phone 7.1

praveen gadipelly

New member
Jun 21, 2012
1
0
0
Visit site
Hi,


I am using HttpWebRequest class for Rest Service call in Windows phone 7.1.I want to use the response of the call back function of service call .but before invoking call back function, next statement in the function that is calling service call is executing .Is there any solution for this?
 

dotnetnelson

New member
Jun 12, 2012
13
0
0
Visit site
You can use a ManualResetEvent to signal a thread to wait. Look it up on MSDN.

Or if you do like above suggested then you can use async as follows:
Code:
await MyAsyncMethod().AsTask().Wait();

Also: I'll note that synchronous calls are highly not recommended. You're much better chaining responses using async/await continuations.
 

CommonBlob

New member
Jul 18, 2011
160
1
0
Visit site
You can use a ManualResetEvent to signal a thread to wait. Look it up on MSDN.

Or if you do like above suggested then you can use async as follows:
Code:
await MyAsyncMethod().AsTask().Wait();

Also: I'll note that synchronous calls are highly not recommended. You're much better chaining responses using async/await continuations.
Im a bit new to using await, but I highly recommend that method.
 

dotnetnelson

New member
Jun 12, 2012
13
0
0
Visit site
await is basically a shorthand for .ContinueWith( .. ) on a Task of T .

Code:
var result = await SomeAsyncFunction();
MessageBox.Show(result.ToString());

When you await SomeAsyncFunction() the method you're currently in immediately returns to the caller. Anything after "await" is chained up into a continuation of the asynchronous operation.

So even though the code looks sequential, anything after "await XXX" will only execute once the asynchronous operation has completed. It allows your code to be less disjointed as a result.

Also to note is that the thread your implicit continuation returns on is the same thread you awaited on. So if you await an async method on the UI thread, it will chain your continuation on the UI thread and do the async operation off thread.

It basically saves some typing:

Code:
Task.Factory.StartNew<string>(() => { return SomeSyncOp(); }).ContinueWith(result => { MessageBox.Show(result) });

Only my continuation doesn't do marshaling back to the UI thread.
 

Members online

Forum statistics

Threads
318,853
Messages
2,237,048
Members
427,576
Latest member
Ghost8787