简体   繁体   中英

Create a new non-UI thread to execute requests in windows phone 8

I use SharpBox 1.2 to authorize with the codes below:

private async void toDropBoxBtn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
      config = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) as DropBoxConfiguration;
      DropBoxRequestToken requestToken = DropBoxStorageProviderTools.GetDropBoxRequestToken(config, "7nu03leznnz6x74", "ex3gge8av7kp9lq");
      //it hangs at the line above ^ (requestToken)
      String AuthorizationUrl = DropBoxStorageProviderTools.GetDropBoxAuthorizationUrl(config, requestToken);
      ICloudStorageAccessToken accessToken = DropBoxStorageProviderTools.ExchangeDropBoxRequestTokenIntoAccessToken(config, "7nu03leznnz6x74", "ex3gge8av7kp9lq", requestToken);
    }

Taking a look at the documentation here states that

The SharpBox library has both synchronous and and asynchronous functions for many operations. Both forms are useful for desktop usage, but the synchronous versions cannot be used directly in Windows Phone apps because these calls block the running UI thread and therefore the whole application.

The asynchronous functions can be used in the UI thread. If needed functions only exist in synchronous variants it is possible to run these in another (non UI) thread and return the results using a callback.

Looks like that's the problem for my code-hang.That's why I decided to create a new thread to handle the requestToken line. Most of the suggestions that I found were:

System.Threading.Thread thread = new System.Threading.Thread(getMyToken);
thread.Start();

with the getMyToken as following:

 private void getMyToken()
    {
        MessageBox.Show("In getMyToken()");
        requestToken = DropBoxStorageProviderTools.GetDropBoxRequestToken(config, "7nu03leznnz6x74", "ex3gge8av7kp9lq");
    }

ofcourse I have declared static DropBoxRequestToken requestToken; and static DropBoxConfiguration config; before the constructor().

So my new code will be:

config = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) as DropBoxConfiguration;
MessageBox.Show("Done 1st!");
System.Threading.Thread thread = new System.Threading.Thread(getMyToken);
thread.Start();
String AuthorizationUrl = DropBoxStorageProviderTools.GetDropBoxAuthorizationUrl(config, requestToken);
MessageBox.Show("Done 2nd!");
ICloudStorageAccessToken accessToken = DropBoxStorageProviderTools.ExchangeDropBoxRequestTokenIntoAccessToken(config, "7nu03leznnz6x74", "ex3gge8av7kp9lq", requestToken);

Yup, I put those MessageBox to track where it goes. The problem is I got "Done 1st!" , then "Done 2nd!" when it's supposed to be "Done 1st!" , "In getMyToken()" then "Done 2nd!" . But right after "Done 2nd!" is the System.UnauthorizedAccessException exception caused by getMyToken method, just right after getting in the method (exception occurs at the MessageBox.Show("In getMyToken()"); line).

I have no idea what's going on. Feel free to ask for more information because I might not describe it clearly in this post. Thank you for your time.

I found it. Put all of the codes into getMyToken and it works, not just a single requestToken. So the code inside my event handler is just:

private async void toDropBoxBtn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
  System.Threading.Thread thread = new System.Threading.Thread(getMyToken);
  thread.Start();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM