ã¹ã¬ãããã¼ã«å´ã§æããä¾å¤ã«æ³¨æ
http://d.hatena.ne.jp/akiramei/20060124/p1
âã¯ãã¡ãã£ã¨èª¬æã足ããªãããªã®ã§è£è¶³ãã¾ãã
using System; using System.Threading; delegate void SilverSkin (); class Program { static void Lance () { throw new Exception ("å±ãâ¦"); } static void Callback (IAsyncResult r) { SilverSkin ss = r.AsyncState as SilverSkin; Console.WriteLine ("In Callback."); try { ss.EndInvoke (r); } catch (Exception) { Console.WriteLine ("ä¾å¤ããã®ã¾ã¾æãã."); throw; } } static void Main () { // 1.Threadã®å ´åã¯ä¾å¤ãçºçãã Thread t = new Thread (new ThreadStart (Lance)); t.IsBackground = true; t.Start (); Console.WriteLine ("ã¹ã¬ããã§çºçããä¾å¤ã¯Unhandled."); Thread.Sleep (1000); // 2.Delegate.BeginInvokeã¯EndInvokeãå¼ã°ãªã㨠// ä¾å¤ãã¹ã¬ãããã¼ã«ã«é£ããã SilverSkin ss = new SilverSkin (Lance); IAsyncResult r = ss.BeginInvoke (null, null); r.AsyncWaitHandle.WaitOne (); Console.WriteLine ("BeginInvokeã ããªãUnhandledã«ãªããªã."); Thread.Sleep (1000); // 3.Delegate.EndInvokeãå¼ã¶ã¨ãä¾å¤ãåãããã r = ss.BeginInvoke (null, null); r.AsyncWaitHandle.WaitOne (); try { ss.EndInvoke (r); } catch (Exception e) { Console.WriteLine ("EndInvoke from Main."); } Console.WriteLine ("EndInvokeãå¼ã¶ã¨ä¾å¤ãé£ã¶."); Thread.Sleep (1000); // 4. Callbackå ã®ä¾å¤ã¯ã¹ã¬ãããã¼ã«ã«é£ããã r = ss.BeginInvoke (new AsyncCallback (Callback), null); r.AsyncWaitHandle.WaitOne (); Console.WriteLine ("Callbackå ã§ä¾å¤ãçºçãã¦ãUnhandledã«ãªããªã."); Thread.Sleep (1000); } } /* çµæ Unhandled Exception: System.Exception: å±ã⦠in <0x00024> Program:Lance () in (wrapper delegate-invoke) System.MulticastDelegate:invoke_void () ã¹ã¬ããã§çºçããä¾å¤ã¯Unhandled. BeginInvokeã ããªãUnhandledã«ãªããªã. EndInvoke from Main. EndInvokeãå¼ã¶ã¨ä¾å¤ãé£ã¶. In Callback. ä¾å¤ããã®ã¾ã¾æãã. Callbackå ã§ä¾å¤ãçºçãã¦ãUnhandledã«ãªããªã. */
- é常ã®ã¹ã¬ããã§ä¾å¤ãçºçããã°ãã©ãã«ããã£ãããããªã
- BeginInvokeãå¼ã³ã£ã±ãªãã®å ´åãä¾å¤ã¯éã«è¬ããã¾ã
- EndInvokeã§ã¹ã¬ããããæããããä¾å¤ãé£ã°ããã¾ã
- Callbackã¯ã¹ã¬ãããã¼ã«å´ãªã®ã§ãä¾å¤ã¯éã«è¬ããã¾ã
ä¾ã«åºãã¦ãã¾ãããTimerã¤ãã³ãã§é£ã°ããä¾å¤ãã¹ã¬ãããã¼ã«å´ãªã®ã§åãéå½ã§ãã