http://msdn.microsoft.com/en-us/library/dd988458.aspxTaskFactory.StartNew() içinde "cancellationToken" nedir?
UPD:
böylece, o sırada bu makaleyi ele alalım:
static void Main()
{
var tokenSource2 = new CancellationTokenSource();
CancellationToken ct = tokenSource2.Token;
var task = Task.Factory.StartNew(() =>
{
// Were we already canceled?
ct.ThrowIfCancellationRequested();
bool moreToDo = true;
Thread.Sleep(5000);
while (moreToDo)
{
// Poll on this property if you have to do
// other cleanup before throwing.
if (ct.IsCancellationRequested)
{
Console.WriteLine("exit");
// Clean up here, then...
ct.ThrowIfCancellationRequested();
}
}
}, tokenSource2.Token); // this parameter useless
Console.WriteLine("sleep");
Thread.Sleep(2000);
Console.WriteLine("cancel");
tokenSource2.Cancel();
// Just continue on this thread, or Wait/WaitAll with try-catch:
try
{
task.Wait();
}
catch (AggregateException e)
{
foreach (var v in e.InnerExceptions)
{
Console.WriteLine(e.Message + " " + v.Message);
}
}
Console.ReadKey();
}
UPD'yi: http://msdn.microsoft.com/en-us/library/dd997396.aspx
Biraz bu kodu değiştirdik Eh, Bu sadece task.IsCanceled
'u değiştirir; hepsini manuel olarak uygulayın.
[Görev İptalini Görün] (http://msdn.microsoft.com/en-us/library/dd997396.aspx). –
Bunu gördüm. "}, tokenSource2.Token)" - bu argüman hiçbir şeyi değiştirmez. Ya bu Token'i aldık ya da almadik - bir istisna elde edecegiz, çünkü ct' kapanis tarafindan ele alinir. – zerkms
Bu soruya gerçek bir cevap gelmediği için çok kötüyüm, aynı zamanda bu argümana duyulan ihtiyacı da merak ediyorum ... şimdilik tamamen işe yaramaz gibi görünüyor. –