当前位置: 代码迷 >> C# >> 关于ManualResetEvent 遏止线程
  详细解决方案

关于ManualResetEvent 遏止线程

热度:172   发布时间:2016-05-05 05:21:50.0
关于ManualResetEvent 阻止线程
 ManualResetEvent resumeEvent = new ManualResetEvent(false);
        volatile bool paused;
        /// <summary>
        /// 阻止线程
        /// </summary>
        void pause()
        {
            resumeEvent.Reset();
            paused = true;
        }
        /// <summary>
        /// 继续线程
        /// </summary>
        void Resume()
        {
            paused = false;
            resumeEvent.Set();
        }
        public void MendthCall(string[] args, int index)
        {
            string str1 = "";
            //获取服务器配置信息
            List<string> list = GetSeverConfig();
            //服务器调用方法
            RemotingServerConn conn = new RemotingServerConn();


            for (int i = 0; i < list.Count; i++)
            {
               
                //连接服务器
                ExecuteCOM ec = conn.ServerConn(list[i]);
                str1 = IsLeisureServer(ec);//查找到空闲状态任务的句柄
                if (str1 != null && str1.Trim() != "")
                {
                    break;
                }
            }
            if (str1 == "")
            {
             
                resumeEvent.WaitOne();
                //pause();
            }             
            try
            {
                for (int i = 0; i < list.Count; i++)
                {
                    //连接服务器
                    ExecuteCOM ec = conn.ServerConn(list[i]);
                    string str = IsLeisureServer(ec);//查找到空闲状态任务的句柄
                    if (str != null && str.Trim() != "")
                    {
                        _ICOMcallback = new ICOMCallBack();
                        _ICOMcallback.OnExecuteComplete_Event += new ExecuteComplete_Event(ExecuteComplete_EventMethod);
                        ec.AddOnlineCallBack(_ICOMcallback);
                        if (index != -1)
                        {
                            args[index] = str;
                        }
                        ec.ExecuteAsyncPreAndCal(args, index);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误提示");
            }
        }
        /// <summary>
        /// 获取返回结果
        /// </summary>
        /// <param name="str"></param>
        void ExecuteComplete_EventMethod(string str)
        {
            if (EventRemotingArgs != null)
            {
                //if (paused)
                //{
                    //Resume();
                resumeEvent.Set();
                resumeEvent.Reset();
                //}
                RemotingResult result = new RemotingResult();
                result.result = str;
                EventRemotingArgs(this, result);
            }
}

我想在接收事件哪里给ManualResetEvent set()  但是set执行完了,阻止的线程并没有执行、我这么写就没问题,下面这个例子没问题,但是上面这个例子就是执行了set() 但是WaitOne()阻止在哪的线程缺没动静。- -

      bool bol = false;
        ManualResetEvent manual = new ManualResetEvent(false);
        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(ss));
            th.Start();
        
            textBox1.Text += "qwe";
           
        }

        void ss()
        {
            //Thread.Sleep(3000);
            this.Invoke(new MethodInvoker(delegate() { textBox1.Text += "123243"; }));
            manual.WaitOne();
            Thread.Sleep(3000);
            this.Invoke(new MethodInvoker(delegate() { textBox1.Text += "456"; })); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            manual.Set();
            manual.Reset();
           
        }

------解决思路----------------------
ManualResetEvent manual = new ManualResetEvent(false);
改成
ManualResetEvent manual = new ManualResetEvent(true);
试试
这个参数是第一次执行是否阻塞