Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

await / async issues

I'm working on Windows 8 CP and discovered that in my app, I'm unable to properly get the new async/await mechanism to work.

This method I am showing you will work when run as a UnitTest (called from a unit test) but when ran normally, it DOES NOT WORK!

StreamSocket _client;

private void Start() {
     SomeMethod();
     SomeOtherMethod();
}

private async void SomeMethod(string sample)
{
    var request = new GetSampleRequestObject(sample);
    byte[] payload = ConvertToByteArray(request, Encoding.UTF8);

    DataWriter writer = new DataWriter(_client.OutputStream);
    writer.WriteBytes(payload);
    await writer.StoreAsync(); // <--- after this executes, it exits the method and continues
    await writer.FlushAsync(); // <--- breakpoint never reaches here, instead
    writer.DetachStream();
}

private void SomeOtherMethod()
{
    string hello = "hello"; // <--- it skips everything and reaches here!
}

What gives?

like image 300
Brian Liang Avatar asked Jul 04 '26 14:07

Brian Liang


1 Answers

I think you have to put await in front of your initial SomeMethod call in the Start function:

await SomeMethod();

like image 186
Daniel Schlößer Avatar answered Jul 07 '26 02:07

Daniel Schlößer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!