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


Download 48.13 Kb.
bet5/6
Sana18.06.2023
Hajmi48.13 Kb.
#1590764
1   2   3   4   5   6
Bog'liq
18-Mavzu- LINQ so’rovlar

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)

Download 48.13 Kb.

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




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