private static void Printer(string message)
        {
            ThreadTest.CheckThread();

            for (int i = 0; i < 20; i++)
            {
                lock (__SyncRoot)
                {
                    Console.Write("id:{0}", Thread.CurrentThread.ManagedThreadId);
                    Console.Write("- msg ({0})", i);
                    Console.WriteLine("\"{0}\"", message);
                    _Messages.Add(message);
                }
                Thread.Sleep(100);

                //try
                //{
                //    Monitor.Enter(__SyncRoot);

                //    Console.Write("id:{0}", Thread.CurrentThread.ManagedThreadId);
                //    Console.Write("- msg ({0})", i);
                //    Console.WriteLine("\"{0}\"", message);
                //    _Messages.Add(message);
                //}
                //finally
                //{
                //    if (Monitor.IsEntered(__SyncRoot))
                //        Monitor.Exit(__SyncRoot);
                //}
            }
            Console.WriteLine("Thread {0} end", Thread.CurrentThread.ManagedThreadId);
        }
Beispiel #2
0
        private static void ProcessMessage(string message)
        {
            ThreadTest.CheckThread();

            for (int i = 0; i < 3; i++)
            {
                lock (__SyncRoot)
                {
                    Console.Write("id:{0}", Thread.CurrentThread.ManagedThreadId);
                    Console.Write("- msg ({0})", i);
                    Console.WriteLine("\"{0}\"", message);
                }
                Thread.Sleep(200);
            }
            //Console.WriteLine("Thread {0} end", Thread.CurrentThread.ManagedThreadId);
        }