2013-04-26 102 views
0

我是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返回的输出值befor exceuting的GetCompleted &它总是错误的结果...

我也尝试manualResetEvent,但它挡着我UI Thread ......所以没有奏效

,所以任何一个有什么想法解决这一问题?

+1

查找'async'和'等待' – 2013-04-26 06:27:11

+0

你可以解释(简而言之)或gv我链接... – 2013-04-26 06:28:27

+0

这里是[链接](https://www.google.com/search?q=webservice+async+await)和[另一个](http ://stackoverflow.com/search?q = [windows-phone-7] + async + webservice) – 2013-04-26 06:31:02

回答

1

是的我建议你使用“await” - “异步”技术。在继续执行代码之前,确保完成该功能。

这里是你的代码应该是什么样子:这里

更多信息 - > 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; 
    } 
} 
+0

即将建议等待我自己 – 2013-04-26 10:41:16

3

异步等待关键字是解决你的情况的一种方式。但是,您的实际问题是您不了解GetAsync调用的工作原理。 当你说:

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 
    } 

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

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

它相当于

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 use dispatcher on WP7