Mavzu: linq so’rovlar. Reja: I. Kirish II. Asosiy qism Parallel linq. Parallel linq ga kirish. AsParallel usuli. AsOrdered usuli


Download 53.3 Kb.
bet6/7
Sana18.06.2023
Hajmi53.3 Kb.
#1591734
1   2   3   4   5   6   7
Bog'liq
Mavzu

{
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)
Console.WriteLine(e.Message);
}
}
finally
{
cts.Dispose();
}
Console.ReadLine();

Download 53.3 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling