/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/runtime-libraries/snippets/System.Threading/Monitor/Overview/csharp/badlock1.cs
62 строки
2 KB
Genevieve Warren
Add remarks for core types (#38970)
05 янв 2024, 22:14
Не верифицирован
05 янв 2024, 22:14
71d1377
Код
Авторство
О чём код?
//<Snippet2> using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; public class Example1 { public static void Main() { int nTasks = 0; List<Task> tasks = new List<Task>(); try { for (int ctr = 0; ctr < 10; ctr++) tasks.Add(Task.Run(() => { // Instead of doing some work, just sleep. Thread.Sleep(250); // Increment the number of tasks. Monitor.Enter(nTasks); try { nTasks += 1; } finally { Monitor.Exit(nTasks); } })); Task.WaitAll(tasks.ToArray()); Console.WriteLine("{0} tasks started and executed.", nTasks); } catch (AggregateException e) { String msg = String.Empty; foreach (var ie in e.InnerExceptions) { Console.WriteLine("{0}", ie.GetType().Name); if (!msg.Contains(ie.Message)) msg += ie.Message + Environment.NewLine; } Console.WriteLine("\nException Message(s):"); Console.WriteLine(msg); } } } // The example displays the following output: // SynchronizationLockException // SynchronizationLockException // SynchronizationLockException // SynchronizationLockException // SynchronizationLockException // SynchronizationLockException // SynchronizationLockException // SynchronizationLockException // SynchronizationLockException // SynchronizationLockException // // Exception Message(s): // Object synchronization method was called from an unsynchronized block of code. // </Snippet2>