private void buttonBegin_Click(object sender, EventArgs e)
        {
           this.demoThread = new Thread(new ThreadStart(dateread));
           this.demoThread.Start();
        }在dateread中有两个循环,一个是读取数组中的值,然后由发送函数将该值发送到硬件板卡,另一个循环在第一个当中,是检查发送函数是否执行完毕的。
在执行上面这个事件时程序会卡,不能执行其他操作,直至将数组中的所有数据发送完。
为什么这样呢?

解决方案 »

  1.   

    第2个循环体中加上
    Thread.Sleep(0) 
    试试
      

  2.   

     加上this.demoThread.IsBackground = true;(); 后台执行试下.
      

  3.   

           private void dateread()
            {
                if (richTextBox1.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(dateread);
                    this.Invoke(d, new object[]{});
                }
                else
                {
                    int s = 0;
                    string strtest;
    //                 for (int i = 0; i < al.Count; i++)
                    for (int i = 0; i < richTextBox1.Lines.Length; i++)
                    {
    //                     strtest = al[i].ToString();
                        richTextBox1.ScrollToCaret();
                        strtest = richTextBox1.Lines[i];
                        richTextBox1.Select(s, richTextBox1.Lines[i].Length);
                        richTextBox1.SelectionColor = System.Drawing.Color.Red;
                        while (GetStatus() != 0)//GetStatus为dll中的函数,查询状态的
                        {
                            Thread.Sleep(50);
                        }
                        if (strtest.IndexOf("X") > -1 || strtest.IndexOf("Y") > -1)
                        {
                            DateProcessor(strtest);
                        }
                        richTextBox1.SelectionColor = System.Drawing.Color.Empty;
                        s += richTextBox1.Lines[i].Length + 1;
                }           }
               // return s;
            }
            private void DateProcessor(string ss)
            {
                int l1=X, l2=Y;
                //string ss = "X1234567890Y1234567890";
                int s = 0;
                int a = ss.IndexOf('X');
                int b= ss.IndexOf('Y');
                if (a>-1)
                {
                    if (b > -1)
                    {
                        l1 = int.Parse(ss.Substring(a + 1, b - a - 1));
                    }
                    else
                    {
                        l1 = int.Parse(ss.Substring(a + 1, ss.Length - a - 1));
                    }
                }
                if (b > -1)
                {
                    l2 = int.Parse(ss.Substring(b + 1, ss.Length - b - 1));
                }
                //while(s!=1)
                //{
                    s = LnFour(l1 - X, l2 - Y, 0, 0);//LnFour为dll中的, 往硬件中发送
                    Thread.Sleep(0);
                    //Thread.Sleep(50);
                //}
                X += (l1 - X);
                Y += (l2 - Y);
    //             while(ENC_GetStatus()!=0)
    //             {
    //                 Thread.Sleep(500);
    //             }
                // MessageBox.Show(ss + "++" + l1.ToString() + "++" + l2.ToString());
                //return s;
            }
    请高人指点~
      

  4.   

    我是猎头公司的angel,目前有.net的相关需求,要求有5年以上的c#.net的相关经验,英文能进行英文面试,base:上海
    感兴趣的请联系我
    email:[email protected]
    msn: [email protected]
      

  5.   

    你写了 Thread.Sleep(50); 当然会卡的啊。
    去掉所有Sleep.
      

  6.   

    我把数据都放到数组里面,然后再处理,还是不行
    确实有个timer刷新界面的,但是把它去掉,也不管用
      

  7.   

    线程可以挂起的   ManualResetEvent  他的set  和reset
      

  8.   

    问题暂时已经解决,在while中假如System.Windows.Forms.Application.DoEvents();O(∩_∩)O谢谢大家关注!