0

I need call another method when awaited ConnectAsync method finish. I try thread pool, cycles etc but I still have the same problem because when start method ConnectAsync then is immediately calling next method but I need first finish ConnectAsync method then call my method. Method ConnectAsync is connecting to server in my web socket client and when Im connected then my method should be called(method navigate to another page). Any idea? .

Awaited method: await socket.ConnectAsync(server)

My method which I want call when awaited finish: Frame.Navigate(typeof(MainContentPage));

Method in my web socket client:

public async void _connect(string token, string idInstalation, string lang)
    {
        try
        {
            if (token!=null){
            socket.SetRequestHeader("Token", token);
            socket.SetRequestHeader("Lang", lang);
            socket.SetRequestHeader("idInstallation", idInstalation);
            }
            await socket.ConnectAsync(server);
            System.Diagnostics.Debug.WriteLine("Connected");
            connected = true;
            writer = new DataWriter(socket.OutputStream);
            messageNumber = 1;
        }
        catch (Exception)
        {
            var dialog = new MessageDialog("Cannot connect to server", "Error").ShowAsync();
        } 
    }

public static void connect(string token, string idInstalation, string lang)
    {
        instance._connect(token,idInstalation,lang);
    }

Mainpage where is calling websocket method:

private void connectMe()
 {
  WebSocketClient.connect(null, null, null);
  Frame.Navigate(typeof(MainContentPage));
 }
pavol.franek
  • 1,396
  • 3
  • 19
  • 42

3 Answers3

2

I have an MSDN article that lays down some guidelines. Among them are "avoid async void" (i.e., use async Task instead of async void). Once you do this, you can await your connection method:

public async Task connectAsync(string token, string idInstalation, string lang)

private async Task connectMeAsync()
{
  await WebSocketClient.connectAsync(null, null, null);
  Frame.Navigate(typeof(MainContentPage));
}

P.S. I strongly recommend you use something like WebAPI or Azure Mobile Services instead of a bare-bones socket. TCP/IP application protocol design is more difficult than it first appears.

Also, I have an async intro that may be helpful.

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

As Steven already pointed out you have to change the signatures of your methods from void to Task. This will allow you to run code after the async method has completed.

    public async Task _connect(string token, string idInstalation, string lang)
    {
        ...
    }

    public static Task connect(string token, string idInstalation, string lang)
    {
        await instance._connect(token,idInstalation,lang);
    }

Then you still have to adjust your calling method:

    private void connectMe()
    {
        await WebSocketClient.connect(null, null, null);
        Frame.Navigate(typeof(MainContentPage));
    }

When working with async you have to await it all the way up to where you have your code that you want to execute after an async call e.g. "turtles all the way down".

Mark
  • 7,891
  • 5
  • 27
  • 36
0

You should refer to the answer provided here. In order to achieve the effect you are interested in, I would call socket.ConnectAsync(server); in a Task then call WaitAll. Here's what I mean:

if (token!=null)
{
   socket.SetRequestHeader("Token", token);
   socket.SetRequestHeader("Lang", lang);
   socket.SetRequestHeader("idInstallation", idInstalation);
}
Task t = Task.Factory.StartNew(()=>{socket.ConnectAsync(server);});
Task.WaitAll(t);
System.Diagnostics.Debug.WriteLine("Connected");
connected = true;
writer = new DataWriter(socket.OutputStream);
messageNumber = 1;
Community
  • 1
  • 1
mrtig
  • 2,217
  • 16
  • 26