C#勉強中2 Semaphore
ちと長いですが。
#region Using directives using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; #endregion namespace Hige1 { class ThreadTest { public static void StaticDo(Object info) { Info i = (Info)info; Console.WriteLine("Thread" + i.num + "Waiting"); i.semaphore.WaitOne(); try { Console.WriteLine("Thread" + i.num + "In Critical Session"); // ここでIOException } catch (IOException e) { Console.WriteLine(e.ToString());//ここでもIOException } Thread.Sleep(300); i.semaphore.Release(); Console.WriteLine("Thread" + i.num + "End"); } } class Info { public Info(Semaphore semaphore, int num) { this.semaphore = semaphore; this.num = num; } public Semaphore semaphore; public int num; } class Hige1 { static void Main(string[] args) { Semaphore semaphore = new Semaphore(0, 5); for (int i = 0; i < 6; i++) { Thread thread = new Thread*1; } Thread.Sleep(500); Console.WriteLine("Main Thread release sempahore"); semaphore.Release(5); } } }
IOException発生理由を知りたいのですがConsole.WriteLineでも起こる模様。
Visual C#上は{"ハンドルが無効です。\r\n"}とか出ているなぁ。。
もう少しがんばってみよう。
*1:new ParameterizedThreadStart(ThreadTest.StaticDo))); thread.Start(new Info(semaphore, i