0

I have created a webservice and a method as follows:

[WebMethod]
public bool GetMasterExit(int RoomID)
{
    if(GameList[RoomID][0] == "\0")
        return true;
    else
        return false;
}

And then I call this from WP client using the following way but there is a problem that I want the main process blocked until the webservice returns the value I want but in this way the value is wrong for delay.

I have tried to use the Await but I got an error that "cannot await void", so Anyone knows how to solve this problem?

public void Test()
{
    ServiceSoapClient GameClient = new ServiceSoapClient();
    GameClient.GetMasterExitCompleted += _clientGetMasterExitCompleted;
    GameClient.GetMasterExitAsync(RoomID);
    Console.WriteLine(MasterExit);
}


public void _clientGetMasterExitCompleted(object sender, GetMasterExitCompletedEventArgs e)
{
    MasterExit = e.Result;
}
Kara
  • 6,115
  • 16
  • 50
  • 57

2 Answers2

0

If you are willing to use await then you must add modifier async to your function signature.Hope this helps.

Use async and await like this:

public async void Test() 
{
    ServiceSoapClient GameClient = new ServiceSoapClient();
    GameClient.GetMasterExitCompleted += _clientGetMasterExitCompleted;
    await GameClient.GetMasterExitAsync(RoomID);
    Console.WriteLine(MasterExit);
}
vITs
  • 1,651
  • 12
  • 30
  • I have updated my code like this: 1. On Web Service: [WebMethod] public Task GetMasterExit(int RoomID) { if(GameList[RoomID][0] == "\0") return true; else return false; } 2. On Windows Phone APP: public async void Test() { ServiceSoapClient GameClient = new ServiceSoapClient(); MasterExit = await GameClient.GetMasterExitAsync(RoomID); Console.WriteLine(MasterExit); } Unfortunately, the re is a error in "MasterExit = await GameClient.GetMasterExitAsync(RoomID);" , saying that "cannot await void". I totally lose it. :( – Morgan2 May 26 '14 at 11:06
  • public async Test() { ServiceSoapClient GameClient = new ServiceSoapClient(); MasterExit = await GameClient.GetMasterExitAsync(RoomID); Console.WriteLine(MasterExit); } Try this as you can not use async with void when you have called await in that function. – vITs May 26 '14 at 12:28
  • No, It doesn't help and I wonder if you can help me modify these two methods? – Morgan2 May 27 '14 at 13:33
  • Hi, vits, In "await GameClient.GetMasterExitAsync(RoomID)" this line, there is an error says that "cannot await void " – Morgan2 May 28 '14 at 03:02
  • Please have a look at [this](http://stackoverflow.com/questions/11803370/using-async-await-inside-void-method) and get understanding of how to use await.Cheers – vITs May 28 '14 at 05:34
0

I want the main process blocked until the webservice returns the value

This is an incorrect approach. Especially on mobile platforms such as WP, you're not allowed to block the UI like that.

The proper solution is to use await. Follow the Task-based Asynchronous Pattern and write an EAP wrapper called GetMasterExitTaskAsync.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810