--- lang: ja-jp breaks: true --- # C# シグナルを使って非同期処理を行う 2021-10-13 ## CountdownEvent 軽量だとか。 > System.Threading.CountdownEvent > https://qiita.com/rawr/items/6888778b53fc95c41f00#systemthreadingcountdownevent > スレッドの相互作用、またはシグナリング > https://docs.microsoft.com/ja-jp/dotnet/standard/threading/overview-of-synchronization-primitives#thread-interaction-or-signaling > CountdownEvent > https://docs.microsoft.com/ja-jp/dotnet/standard/threading/countdownevent ```csharp= IEnumerable<Data> source = GetData(); using (CountdownEvent e = new CountdownEvent(1)) { // fork work: foreach (Data element in source) { // Dynamically increment signal count. e.AddCount(); ThreadPool.QueueUserWorkItem( delegate(object state) { try { ProcessData(state); } finally { e.Signal(); } }, element ); } e.Signal(); // The first element could be run on this thread. // Join with work. e.Wait(); } // .,. ``` ## ManualResetEvent ###### tags: `C#` `シグナル` `非同期` `マルチスレッド` `CountdownEvent`