如下,function3中的set执行完毕后,系统会随机执行function1的.WaitOne()或者function2的.WaitOne()  ,如果执行的 是 function1的.WaitOne(),则系统将不会有任何输出,会无限制等待开启信号,如果执行的是  function2的.WaitOne()一切正常,按照执行顺序 function3--->function2-->function1我想用AutoResetEvent人为的控制线程的执行顺序(ManualResetEvent方法试过可以),该怎么做呢??
 static AutoResetEvent mre = new AutoResetEvent(false);
        static AutoResetEvent mre1 = new AutoResetEvent(false);
        static void Main(string[] args)
        {
            funDelegate1();
        }
        static private void funDelegate1()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(function1));
            ThreadPool.QueueUserWorkItem(new WaitCallback(function2));
            ThreadPool.QueueUserWorkItem(new WaitCallback(function3));
            Console.Read();
        }
        private static void function1(object funParam)
        {
            mre.WaitOne();
            mre1.WaitOne();
            StackTrace st = new StackTrace(true);
            Console.WriteLine(st.GetFrame(0).GetMethod().Name);
        }
        private static void function2(object funParam)
        {
            mre.WaitOne();
            mre1.Set();
            StackTrace st = new StackTrace(true);
            Console.WriteLine(st.GetFrame(0).GetMethod().Name);
        }
        private static void function3(object funParam)
        {
            StackTrace st = new StackTrace(true);
            Console.WriteLine(st.GetFrame(0).GetMethod().Name);
            mre.Set();
        }

解决方案 »

  1.   


    private static void function1(object funParam)
            {
                mre1.WaitOne();
                StackTrace st = new StackTrace(true);
                Console.WriteLine(st.GetFrame(0).GetMethod().Name);
            }
            private static void function2(object funParam)
            {
                mre.WaitOne();
                StackTrace st = new StackTrace(true);
                Console.WriteLine(st.GetFrame(0).GetMethod().Name);
                mre1.Set();
            }
            private static void function3(object funParam)
            {
                StackTrace st = new StackTrace(true);
                Console.WriteLine(st.GetFrame(0).GetMethod().Name);
                mre.Set();
            }