有没有人能够让CancellationToken适用于AuthorizeAsync()?
以下作品:
private void TryToAuthenticate()
{
//Cancel the task BEFORE it starts
m_cancelTokSrc.Cancel();
//Start task
Task<UserCredential> task = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
m_cancelTokSrc.Token,
new FileDataStore(m_CredentialFilePath, true))
.ContinueWith((t) => AuthenticateCompleted(t));
}
private UserCredential AuthenticateCompleted(Task<UserCredential> task)
{
//FIRES JUST FINE!
UserCredential credentials = null;
switch (task.Status)
{
case TaskStatus.Canceled:
return null;
case TaskStatus.RanToCompletion:
credentials = task.Result;
break;
}
return credentials;
}
以下不起作用:
private void TryToAuthenticate()
{
//Start task
Task<UserCredential> task = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
m_cancelTokSrc.Token,
new FileDataStore(m_CredentialFilePath, true))
.ContinueWith((t) => AuthenticateCompleted(t));
//Cancel the task AFTER it starts
m_cancelTokSrc.Cancel();
}
private UserCredential AuthenticateCompleted(Task<UserCredential> task)
{
//DOES NOT FIRE!!!
UserCredential credentials = null;
switch (task.Status)
{
case TaskStatus.Canceled:
return null;
case TaskStatus.RanToCompletion:
credentials = task.Result;
break;
}
return credentials;
}
似乎每当我请求授权时,它就会挂起等待来自Web浏览器的响应或其他内容。在循环中,它等待授权回复,它似乎永远不会检查cancelTokenSource.Token.IsCancellationRequested
。有没有人成功取消过请求?