計時超時事件明確是不是一個好方法。你可能想看看 Cancel Async Tasks after a Period of Time。
您可以通過 使用CancellationTokenSource.CancelAfter方法取消一段時間後異步操作,如果你不想做 等待操作完成。此方法調度 取消未在 一段時間年代由CancelAfter表達指定內完成任何相關的任務。從MSDN
樣品的編號:
// Declare a System.Threading.CancellationTokenSource.
CancellationTokenSource cts;
private async void startButton_Click(object sender, RoutedEventArgs e)
{
// Instantiate the CancellationTokenSource.
cts = new CancellationTokenSource();
resultsTextBox.Clear();
try
{
// ***Set up the CancellationTokenSource to cancel after 2.5 seconds. (You
// can adjust the time.)
cts.CancelAfter(2500);
await AccessTheWebAsync(cts.Token);
resultsTextBox.Text += "\r\nDownloads succeeded.\r\n";
}
catch (OperationCanceledException)
{
resultsTextBox.Text += "\r\nDownloads canceled.\r\n";
}
catch (Exception)
{
resultsTextBox.Text += "\r\nDownloads failed.\r\n";
}
cts = null;
}
// You can still include a Cancel button if you want to.
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
if (cts != null)
{
cts.Cancel();
}
}
async Task AccessTheWebAsync(CancellationToken ct)
{
// Declare an HttpClient object.
HttpClient client = new HttpClient();
// Make a list of web addresses.
List<string> urlList = SetUpURLList();
foreach (var url in urlList)
{
// GetAsync returns a Task<HttpResponseMessage>.
// Argument ct carries the message if the Cancel button is chosen.
// Note that the Cancel button cancels all remaining downloads.
HttpResponseMessage response = await client.GetAsync(url, ct);
// Retrieve the website contents from the HttpResponseMessage.
byte[] urlContents = await response.Content.ReadAsByteArrayAsync();
resultsTextBox.Text +=
String.Format("\r\nLength of the downloaded string: {0}.\r\n"
, urlContents.Length);
}
}
還有Thread.Abort Method終止該線程。
編輯:取消任務 - 一個更好的解釋(source)
Task類提供了一種方法來取消基礎上,CancellationTokenSource類的啓動任務。
步驟來取消任務:
異步方法應該除外類型的參數CancellationToken
創建像CancellationTokenSource類的實例:var cts = new CancellationTokenSource();
從傳遞的CancellationToken所述instace到異步方法,如:Task<string> t1 = GreetingAsync("Bulbul", cts.Token);
從長時間運行的方法中,我們必須調用CancellationToken的ThrowIfCancellationRequested()方法。
static string Greeting(string name, CancellationToken token)
{
Thread.Sleep(3000);
token. ThrowIfCancellationRequested();
return string.Format("Hello, {0}", name);
}
趕上OperationCanceledException
從那裏我們awiting的任務。
我們可以通過調用CancellationTokenSource
實例Cancel
方法取消操作,OperationCanceledException
將從長時間運行的操作被拋出。我們也可以設置時間來取消對instanc的操作。
更多詳細信息 - MSDN Link。
static void Main(string[] args)
{
CallWithAsync();
Console.ReadKey();
}
async static void CallWithAsync()
{
try
{
CancellationTokenSource source = new CancellationTokenSource();
source.CancelAfter(TimeSpan.FromSeconds(1));
var t1 = await GreetingAsync("Bulbul", source.Token);
}
catch (OperationCanceledException ex)
{
Console.WriteLine(ex.Message);
}
}
static Task<string> GreetingAsync(string name, CancellationToken token)
{
return Task.Run<string>(() =>
{
return Greeting(name, token);
});
}
static string Greeting(string name, CancellationToken token)
{
Thread.Sleep(3000);
token.ThrowIfCancellationRequested();
return string.Format("Hello, {0}", name);
}
請格式化您的代碼。 –
什麼不是格式化的? –
這是您的代碼在您的IDE中的外觀? –