Delegate.BeginInvokeã«éãå¦çã¯ããããª
Delegateã使ã£ã¦æ軽ã«éåæå¦çãè¡ããã¨ãåºæ¥ã¾ãããããã«ã¯è½ã¨ãç©´ãããã¾ãã
using System; using System.Threading; delegate void Rush(); class Program { static void TheWorld() { Console.WriteLine("\nã¶ã»ã¯ã¼ã«ãï¼!ãæãæ¢ã¾ãï¼\n"); while (true) Thread.Sleep(Int16.MaxValue); } static void Muda() { Console.Write("ç¡é§"); while (true) Thread.Sleep(Int16.MaxValue); } static void Mudaa() { Console.WriteLine("ç¡é§ã¡ï¼ï¼ï¼ï¼"); Thread.Sleep(1000); Console.WriteLine("\nããã¦æã¯åãåºã\n"); Thread.Sleep(1000); } static void Time(object o, System.Timers.ElapsedEventArgs e) { Console.WriteLine(e.SignalTime); } static void Main(string[] args) { System.Timers.Timer t = new System.Timers.Timer(); t.Elapsed += new System.Timers.ElapsedEventHandler(Time); t.Interval = 1000; t.Start(); Thread.Sleep(3000); new Rush(TheWorld).BeginInvoke(null, null); for (int i = 0; i < 23; ++i) new Rush(Muda).BeginInvoke(null, null); // ãã£ã¡ã ã¨æã¯æ¢ã¾ãã¾ãã // new Thread(new ThreadStart(Muda)).Start(); IAsyncResult result = new Rush(Mudaa).BeginInvoke(null, null); result.AsyncWaitHandle.WaitOne(); Thread.Sleep(1000); } } /* 2006/01/23 22:43:16 2006/01/23 22:43:17 ã¶ã»ã¯ã¼ã«ãï¼!ãæãæ¢ã¾ãï¼ ç¡é§ç¡é§(以ä¸ç¥)ç¡é§ã¡ï¼ï¼ï¼ï¼ ããã¦æã¯åãåºã 2006/01/23 22:43:31 (以ä¸ç¥) 2006/01/23 22:43:32 */
ä¸è¨ãè¦ã¦ã®éãã大éã«BeginInvokeãè¡ãã¨Timers.Elapsedãå½±é¿ãåãã¾ããå®ã¯BeginInvokeãElapsedãã¹ã¬ãããã¼ã«ã使ç¨ãã¦ãã¦ãããã©ã«ãã§1CPUã1ããã»ã¹ããã25ã¹ã¬ããã¨ããå¶éãããããã§ãããªã®ã§ãBeginInvokeã«ã¯æéã®æããå¦çã¯è¡ãããã¹ãã§ã¯ããã¾ãããã¡ãªã¿ã«ã³ã¡ã³ãã¢ã¦ããã¦ããThreadã使ã£ãå¦çã®å ´åã¯ãã¹ã¬ãããã¼ã«ã使ããªãã®ã§æã¯æ¢ã¾ãã¾ããã(^^;