예제 #1
0
        public static void Main(string[] args)
        {
            var banana = GetBananaASCII();

            var thread = new Thread((opts) =>
            {
                var threadStartOptions = opts as ThreadStartOptions;
                if (threadStartOptions.IsNull())
                {
                    throw new ArgumentException("Invalid ThreadStartOptions argument.");
                }

                // Infinite loop on the new thread
                while (true)
                {
                    Thread.Sleep(threadStartOptions.SleepTime);
                    Console.ForegroundColor = threadStartOptions.ForegroundColor;
                    Console.WriteLine(threadStartOptions.Message);
                }
            });

            // Setup and run the new thread
            var threadStartOpions = new ThreadStartOptions
            {
                Message         = banana,
                SleepTime       = 1000,
                ForegroundColor = ConsoleColor.Yellow
            };

            thread.Start(threadStartOpions);

            // Run another infinite loop on the main thread
            while (true)
            {
                Thread.Sleep(200);
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Main thread working now");
            }
        }
예제 #2
0
        public static void Main(string[] args)
        {
            var banana = GetBananaASCII();

            var thread = new Thread((opts) =>
            {
                var threadStartOptions = opts as ThreadStartOptions;
                if (threadStartOptions.IsNull())
                {
                    throw new ArgumentException("Invalid ThreadStartOptions argument.");
                }

                // Infinite loop on the new thread
                while (true)
                {
                    Thread.Sleep(threadStartOptions.SleepTime);
                    Console.ForegroundColor = threadStartOptions.ForegroundColor;
                    Console.WriteLine(threadStartOptions.Message);
                }
            });

            // Setup and run the new thread
            var threadStartOpions = new ThreadStartOptions
            {
                Message = banana,
                SleepTime = 1000,
                ForegroundColor = ConsoleColor.Yellow
            };
            thread.Start(threadStartOpions);

            // Run another infinite loop on the main thread
            while (true)
            {
                Thread.Sleep(200);
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Main thread working now");
            }
        }