Share via


Cannot await 'Void'

Question

Tuesday, March 31, 2015 10:11 AM

        private async void connectButton_Click(object sender, RoutedEventArgs e)
        {
            bool connected = false;
            try
            {
                var authClient = new LiveAuthClient("RemovedforWeb");
                LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" });

                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    connected = true;
                    var connectClient = new LiveConnectClient(result.Session);
                    var meResult = await connectClient.GetAsync("me");
                    dynamic meData = meResult.Result;

                }

I remeber this was working before, it is builded for WPhone 8.1 any workarounds it displays cannot await 'void'

Jyrka

                   

                   

                   

                  

                }

                }

All replies (16)

Wednesday, April 1, 2015 3:17 PM âś…Answered | 1 vote

You probably have to download the SDK.

http://www.microsoft.com/en-us/download/details.aspx?id=28359

Mark as answer or vote as helpful if you find it useful | Igor


Tuesday, March 31, 2015 10:57 AM | 1 vote

Hello Jyrka,

The solution is to use async Task. You should avoid async void for several reasons, one of which is composability. If the method cannot be made to return Task (e.g., it's an event handler), then you can use
SemaphoreSlim to have the method signal when it is about to exit. Consider doing this in a finally block.

Hope, this will help you out.

If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.


Tuesday, March 31, 2015 11:12 AM

Hi

Thank you for your answer

I tried to implement Task async, but it seems i get something wrong can you make sample from my code actually how to implement Task async, maybye then i see what i did wrong 


Tuesday, March 31, 2015 11:23 AM | 1 vote

Your code

 private async System.Threading.Tasks.Task connectButton_Click(object sender, RoutedEventArgs e)
        {
            bool connected = false;
            try
            {
                var authClient = new LiveAuthClient("RemovedforWeb");
                LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" });

                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    connected = true;
                    var connectClient = new LiveConnectClient(result.Session);
                    var meResult = await connectClient.GetAsync("me");
                    dynamic meData = meResult.Result;

                }

-Igor


Tuesday, March 31, 2015 11:46 AM

Still getting exactly same error, so maybye it is something else ? any ideas using VS Express for Windows and developing for WP 8.1 (used trial of ultimate to develop it, it worked before ultimate trial ended) ?

Thanks in advance


Tuesday, March 31, 2015 12:16 PM

Also look at the answer about using SemaphoreSlim provided by Tejas TIGate. Here is a link as to how you could implement this, it has a great example in the code.

Using SemaphoreSlim

If that does not work could you copy/paste your complete error message in this thread so we can better assist?

-Igor


Tuesday, March 31, 2015 12:18 PM

Here it is

Error 2 Cannot await 'void' C:\Users\jyrgen\Documents\Visual Studio 2013\Projects\StreamUploadDownload\Page1.xaml.cs 207 55 Microsoft.Live.WP8.Samples.StreamUploadDownload

Thanks in advance

Jyrka


Tuesday, March 31, 2015 12:42 PM

207 is the line number. Can you reply with what code is written on that line number? Is it in the same method you posted earlier? 

What is the error message you get when you change the keyword void to System.Threading.Tasks.Task ?

-Igor


Tuesday, March 31, 2015 12:53 PM

Hey

If i change to System.Threading.Tasks.Task, then it says exactly the same error for all 3 errors

It is one of three await problem all similar

//Line 207
 LiveOperationResult operationResult = await this.liveClient.UploadAsync("/me/skydrive", fileName, e.ImageStream , OverwriteOption.Overwrite );
//270 
await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" });
//Line 276
 var meResult = await connectClient.GetAsync("me");

Thanks in advance !

Jyrka


Tuesday, March 31, 2015 12:56 PM

So you now have the async keyword and the Task object in the method signature like so correct?

protected async System.Threading.Tasks.Task connectButton_Click(object sender, RoutedEventArgs e)
{
// your code
}

You now mentioned you have 3 errors instead of one. Can you please copy/paste the new error message?

-Igor


Tuesday, March 31, 2015 1:05 PM

yes, and error notification is still the same (which is super strange )

I wanted to post pic but it seems i cant.

Thanks in advance

Jyrka


Tuesday, March 31, 2015 1:12 PM

Error 2 Cannot await 'void' C:\Users\jyrgen\Documents\Visual Studio 2013\Projects\StreamUploadDownload\Page1.xaml.cs 207 55 Microsoft.Live.WP8.Samples.StreamUploadDownload

Error 3 Cannot await 'void' C:\Users\jyrgen\Documents\Visual Studio 2013\Projects\StreamUploadDownload\Page1.xaml.cs 270 42 Microsoft.Live.WP8.Samples.StreamUploadDownload

Error 4 Cannot await 'void' C:\Users\jyrgen\Documents\Visual Studio 2013\Projects\StreamUploadDownload\Page1.xaml.cs 276 36 Microsoft.Live.WP8.Samples.StreamUploadDownload

forgot to add errors, but it allways were three oh actually one has popped up more

Error 1 'System.Threading.Tasks.Task StreamUploadDownload.Page1.cam_CaptureImageAvailable(object, Microsoft.Devices.ContentReadyEventArgs)' has the wrong return type C:\Users\jyrgen\Documents\Visual Studio 2013\Projects\StreamUploadDownload\Page1.xaml.cs 105 51 Microsoft.Live.WP8.Samples.StreamUploadDownload

 private async System.Threading.Tasks.Task cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)

//Does some stuf

 LiveOperationResult operationResult = await this.liveClient.UploadAsync("/me/skydrive", fileName, e.ImageStream , OverwriteOption.Overwrite ); //error

        protected async System.Threading.Tasks.Task connectButton_Click(object sender, RoutedEventArgs e)
        {
            bool connected = false;
            try
            {
                var authClient = new LiveAuthClient("Removed from web");
                LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" });
//error
                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    connected = true;
                    var connectClient = new LiveConnectClient(result.Session);
                    var meResult = await connectClient.GetAsync("me"); //error
                    dynamic meData = meResult.Result;

//line 105 new one

_cam.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureImageAvailable); //error

Tuesday, March 31, 2015 1:27 PM

  1. You had the signature right, it should be async void for the event handler in wpf
  2. there are some tasks that are not possible if you target windows phone. Find which line gives the error and focus on that line item. I am not sure if async void is supported in windows phone platform or not so maybe that is the culprit. In the later case remove the async and await keywords. hopefully someone else who has experience writing for that platform can answer or you can ask your question on a forum section specific to that platform.

-Igor


Tuesday, March 31, 2015 2:32 PM

It's the thing you're awaiting which has to return something other than void.

This:

      private async void connectButton_Click(object sender, RoutedEventArgs e)
 

Is a valid click handler.

This:

     protected async System.Threading.Tasks.Task connectButton_Click(object sender, RoutedEventArgs e)
 

Is a bad idea. I've only done very little app store development but a click event doesn't have that signature.

Unless you're awaiting  a click handler, which seems pretty flippin unlikely.

Note that this

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh968445.aspx

Has a regular wpf like handler

private async void SignInClick(object sender, RoutedEventArgs e)
{
  // This call will sign the user in and update the Account flyout UI.
  await SetNameField(true);
}

Which then awaits a task.

private async Task SetNameField(Boolean login) {
    // If login == false, just update the name field. 
    await App.updateUserName(this.userName, login);

Hope that helps.
Recent Technet articles: Property List Editing; Dynamic XAML


Tuesday, March 31, 2015 3:33 PM

Andy - that is what the OP originally had, a method signature with async void. For some reason the OP claims that VS generates the error that it cannot be awaited because of void. I can't reproduce it but thought maybe had something to do with developing against the windows phone platform.

-Igor


Wednesday, April 1, 2015 5:20 AM

Hey

Thanks for all the answers i think i know what the probleem might be, When i used Visual Studio Ultimate edition, I could develop on Silverlight platform for w8.1 where it allowed this stuff, So the question is Express do i have to download something to get this or, upgrade to proffessional ?