コード例 #1
0
        private static void Main(string[] args)
        {
            Runnable runnable = new Runnable();

            runnable.RequireStart();
            Thread outer = new Thread(() =>
            {
                try
                {
                    while (IsProgramActive)
                    {
                        Console.WriteLine("Outer Thread Running...");
                        Thread.Sleep(2000);

                        Thread inner = new Thread(() => runnable.Run())
                        {
                            IsBackground = true
                        };
                        inner.Start();
                    }
                }
                catch (ThreadInterruptedException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    Console.WriteLine("Outer Thread Stopped.");
                }
            })
            {
                IsBackground = true
            };

            outer.Start();

            string command = Console.ReadLine();

            if (command == "")
            {
                runnable.RequireStop();
            }

            Console.ReadLine();
        }