等待完成(web服务)

本文关键字:服务 web 等待 | 更新日期: 2023-09-27 18:10:54

我正在研究web services
下面是我写的调用web service的方法

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

CheckIfUserExist方法调用web服务并返回输出值(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;
     }
 }

但是CheckIfUserExist在执行GetCompleted &它总是出错的结果…

我也试过manualResetEvent,但它阻塞了我的UI Thread…not worked

谁有办法解决这个问题?

等待完成(web服务)

Async Await关键字是解决这种情况的一种方法。然而,你的实际问题是你不明白GetAsync调用是如何工作的。当你说:

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;
     }
 }

相当于:

    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
    }

你有两种可能:如果你的client对象的GetCompleted事件发生在UI线程上,你可以这样做:

 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 <---
         }

如果GetCompleted事件没有在UI线程中发生:

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 <---
}

另外,在调用GetAsync

之前,请注意订阅事件
client.GetCompleted += (o, e) => { ... } //subscribe first
client.GetAsync("me");  // call GetAsync later

如果在WP7上-您可能有Dispatcher.Invoke的问题,请参阅:Can't使用dispatcher在WP7

是的,我建议您使用"await"-"async"技术。在继续编写代码之前,确保函数已经完全完成。

你的代码应该是这样的:

更多信息在这里->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;
     }
 }