Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Async Await keywords are one way to solve your situation. However your actual problem is you dont understand how the <code>GetAsync</code> call works. When you say:</p> <pre><code>public static long CheckIfUserExist() { long UserID = -1; client.GetAsync("me"); client.GetCompleted += (o, e) =&gt; { // some code if (Convert.ToInt64(eargs.Result) == 0) { UserID = Convert.ToInt64(eargs.Result); } return UserID; } } </code></pre> <p>It is equivalent to:</p> <pre><code> public static long CheckIfUserExist() { long UserID = -1; client.GetAsync("me"); client.GetCompleted += MyEventHandler; } void MyEventHandler(object sender, SomeEventArgs e) { // some code if (Convert.ToInt64(eargs.Result) == 0) { UserID = Convert.ToInt64(eargs.Result); } return UserID; // &lt;-- WHAT IS POINT OF RETURNING UserID FROM HERE?? // method maybe running on some other thread asynchronously to UI thread } </code></pre> <p>There are two possibilities for you: If your <code>client</code> object's <code>GetCompleted</code> event occurs on the UI thread you can do this:</p> <pre><code> client.GetCompleted += (o, e) =&gt; { // some code if (Convert.ToInt64(eargs.Result) == 0) { UserID = Convert.ToInt64(eargs.Result); } // your logic here if (UserID == -1) // WRONG RESULT &lt;--- else // RIGHT RESULT &lt;--- } </code></pre> <p>If <code>GetCompleted</code> event does not occur on UI thread:</p> <pre><code>client.GetCompleted += (o, e) =&gt; { // some code if (Convert.ToInt64(eargs.Result) == 0) { UserID = Convert.ToInt64(eargs.Result); } // let UI thread know we've got the result Dispatcher.Invoke( (Action)(() =&gt; { NotifyUIThread(UserID) } )); } ... void NotifyUIThread(long UserId) //This runs on UI thread { if (UserID == -1) // WRONG RESULT &lt;--- else // RIGHT RESULT &lt;--- } </code></pre> <p>Also, take care there you subscribe to event before you call <code>GetAsync</code></p> <pre><code>client.GetCompleted += (o, e) =&gt; { ... } //subscribe first client.GetAsync("me"); // call GetAsync later </code></pre> <p>If on WP7 - you may have problem with <code>Dispatcher.Invoke</code> see this: <a href="https://stackoverflow.com/questions/4247729/cant-use-dispatcher-on-wp7">Can&#39;t use dispatcher on WP7</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload