try
{
factorials.ForAll(n => Console.WriteLine(n));
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine(e.Message);
}
}
Massiv qatorni o'z ichiga olganligi sababli, translatsiyaga urinish muvaffaqiyatsiz tugadi va dastur Visual Studio'da nosozliklarni tuzatish rejimida ishga tushirilganda, ijro transformatsiya qatorida to'xtaydi. Va davom etgandan so'ng, istisno catch blokida ushlanadi va konsolda xato xabari ko'rsatiladi.
Parallel operatsiyani bekor qilish. Ehtimol, operatsiyani yakunlashdan oldin tugatishimiz kerak bo'lishi mumkin. Bunday holda, CancellationToken parametr sifatida uzatiladigan WithCancellation() usulidan foydalanishimiz mumkin:
static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
new Task(() =>
{
Thread.Sleep(400);
cts.Cancel();
}).Start();
try
{
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, };
var factorials = from n in numbers.AsParallel().WithCancellation(cts.Token)
select Factorial(n);
foreach (var n in factorials)
Console.WriteLine(n);
}
catch(OperationCanceledException ex)
{
Console.WriteLine("Operatsiya to'xtatildi ");
}
catch (AggregateException ex)
{
if (ex.InnerExceptions != null)
{
foreach (Exception e in ex.InnerExceptions)
Do'stlaringiz bilan baham: |