1

I am basically trying to run my authorization process for my WinRt app synchronously, but everything I've tried appears to deadlock the app after authorization on the MainPage, preventing navigation to the HomePage. Can't seem to figure it out. Here is my code without modifications, which works perfectly fine asynchronously below. How can I make this work synchronously:

Class:

public async void InitializeTwitterAsnc()
    {

        WinRtAuthorizer authorizer = null;

        authorizer = new WinRtAuthorizer()
        {
            Credentials = new LocalDataCredentials()
            {
                ConsumerKey = "blah379",
                ConsumerSecret = "blah123"
            },
            UseCompression = true,
            Callback = new Uri("http://blah.com")
        };

        if(!authorizer.IsAuthorized)
        {
            await authorizer.AuthorizeAsync();
        }

        // set the twitter credential fields
        await Task.Run
            (() =>
                 {
                     twitterName = authorizer.Credentials.ScreenName;
                     twitterId = authorizer.Credentials.UserId;
                     accessToken = authorizer.Credentials.AccessToken;
                     oAuthToken = authorizer.Credentials.OAuthToken;

                     this.TwitterContext = new TwitterContext(authorizer);
                 });
    }

code that calls the method from MainPage.xaml.cs:

private void StartTwitterLogin(object sender, RoutedEventArgs e)
    {
        // start the twitter authorization
        TwitterAuth twitAuth = new TwitterAuth();
        twitAuth.InitializeTwitterAsnc();


        this.Frame.Navigate(typeof (HomePage));
    }
Charles
  • 50,943
  • 13
  • 104
  • 142
user1206480
  • 1,798
  • 3
  • 28
  • 45

2 Answers2

3

You can't. Asynchronouse APIs are only meant to be run asynchronously and if you try to force your thread to wait for the result - you will often get a deadlock since the APIs themselves often need to run something on the thread you are blocking. You shouldn't try to fight it and rather think what the actual problem you are trying to solve is.

In your case you should change the signature of your method from

public async void InitializeTwitterAsnc()

to

public async Task InitializeTwitterAsnc()

so that it can be awaited and then await it here:

private async Task StartTwitterLogin(object sender, RoutedEventArgs e)
{
    // start the twitter authorization
    TwitterAuth twitAuth = new TwitterAuth();
    await twitAuth.InitializeTwitterAsnc();


    this.Frame.Navigate(typeof (HomePage));
}
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • Wow, I didn't see that before. Thank you for bringing that to my attention. I think I just initially over thought the problem, being new to async and all. – user1206480 Jan 26 '13 at 11:08
  • How exactly are you using await in a non-async method? – SBoss Feb 12 '13 at 13:41
0

So ultimately, my solution was to handle the navigation inside the async twitAuth.InitializeTwitterAsync() method. In order for the navigation to work, I had to create a static frame property in the app.xaml.cs that I could use in my custom class for navigation. See this link WinRt page navigaiton .

Community
  • 1
  • 1
user1206480
  • 1,798
  • 3
  • 28
  • 45