简体   繁体   中英

wait until get completed (web service)

i am working on web services ...
below is the method i have written to call a web service

 long UserID = CheckIfUserExist(temp);       
    if (UserID == -1)
           // WRONG RESULT <---
    else
          // RIGHT RESULT <---

the CheckIfUserExist method calling that web service and returning the output value ( UserID )--->

public static long CheckIfUserExist()
{                
     long UserID = -1;
     client.GetAsync("me");  
     client.GetCompleted += (o, e) =>
     {
         // some code
         if (Convert.ToInt64(eargs.Result) == 0)
         {
               UserID = Convert.ToInt64(eargs.Result);
         }
         return UserID;
     }
 }

but the CheckIfUserExist returning an output value befor exceuting the GetCompleted & its always going wrong result...

i also tried manualResetEvent , but its blocking my UI Thread ... so not worked

so any one have any idea to fix this ?

Async Await keywords are one way to solve your situation. However your actual problem is you dont understand how the GetAsync call works. When you say:

public static long CheckIfUserExist()
{                
     long UserID = -1;
     client.GetAsync("me");  
     client.GetCompleted += (o, e) =>
     {
         // some code
         if (Convert.ToInt64(eargs.Result) == 0)
         {
               UserID = Convert.ToInt64(eargs.Result);
         }
         return UserID;
     }
 }

It is equivalent to:

    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; // <-- WHAT IS POINT OF RETURNING UserID FROM HERE?? 
                        // method maybe running on some other thread asynchronously to UI thread
    }

There are two possibilities for you: If your client object's GetCompleted event occurs on the UI thread you can do this:

 client.GetCompleted += (o, e) =>
         {
             // some code
             if (Convert.ToInt64(eargs.Result) == 0)
             {
               UserID = Convert.ToInt64(eargs.Result);
             }
             // your logic here
             if (UserID == -1)
                  // WRONG RESULT <---
             else
                  // RIGHT RESULT <---
         }

If GetCompleted event does not occur on UI thread:

client.GetCompleted += (o, e) =>
             {
                 // 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)(() => { NotifyUIThread(UserID) } ));
             }
...

void NotifyUIThread(long UserId) //This runs on UI thread
{
    if (UserID == -1)
       // WRONG RESULT <---
    else
       // RIGHT RESULT <---

}

Also, take care there you subscribe to event before you call GetAsync

client.GetCompleted += (o, e) => { ... } //subscribe first
client.GetAsync("me");  // call GetAsync later

If on WP7 - you may have problem with Dispatcher.Invoke see this: Can't use dispatcher on WP7

Yes I suggest you use the "await"-"async" technology . To make sure that the function is completly finished before continuing code .

Here is what your code should look like :

more info here -> http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

public async void updateUser()
{
   long UserID = await CheckIfUserExist(temp);       
   if (UserID == -1)
       // WRONG RESULT <---
   else
      // RIGHT RESULT <---
}

public async Task<long> CheckIfUserExist()
{                
     long UserID = -1;
     await client.GetAsync("me");  
     client.GetCompleted += (o, e) =>
     {
         // some code
         if (Convert.ToInt64(eargs.Result) == 0)
         {
               UserID = Convert.ToInt64(eargs.Result);
         }
         return UserID;
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM