我的程序如下,我用到了委托,为什么程序会在while语句中死锁?使整个界面停滞?
但如果不用委托,直接用线程运行AlarmThreadEvent(),就不会停滞,这是为什么?
因为我要操作窗口,所以得用委托呀,请帮一下,急,在线等;
我的程序如下
        private void button1_Click(object sender, EventArgs e)
        {
                    AlarmThread = new Thread(new ThreadStart(AlarmThreadEventA));
                    AlarmThread.IsBackground = true;        }        public delegate void AlarmThreadEventdelegate();
        private void AlarmThreadEventA()
        {
            AlarmThreadEventdelegate a = new AlarmThreadEventdelegate(AlarmThreadEvent);
            this.BeginInvoke(a, new object[]{ });
        }
        private void AlarmThreadEvent()
        {
            while (true)
            {
                Thread.Sleep(1);
            }
        }

解决方案 »

  1.   

    不好意思写错了
            private void button1_Click(object sender, EventArgs e)
            {
                        AlarmThread = new Thread(new ThreadStart(AlarmThreadEventA));
                        AlarmThread.Start()        } 
      

  2.   

    Thread.Sleep(1);  让线程停止1豪秒 这样CPU有时间处理其他的内容..
    否则你 while()一直在运行 CPU就没时间执行别的.你可以考虑在循环里Application.DoEvents();
      

  3.   

    下面的代码示例演示通过静态方法和实例方法创建和使用 ParameterizedThreadStart 委托的语法。Visual Basic  复制代码 
    Imports System
    Imports System.ThreadingPublic Class Work    <MTAThread> _
        Shared Sub Main()
            ' To start a thread using a shared thread procedure, use
            ' the class name and method name when you create the 
            ' ParameterizedThreadStart delegate. Visual Basic expands
            ' the AddressOf expression to the appropriate delegate 
            ' creation syntax:
            '    New ParameterizedThreadStart(AddressOf Work.DoWork)
            '
            Dim newThread As New Thread(AddressOf Work.DoWork)
            
            ' Use the overload of the Start method that has a
            ' parameter of type Object. You can create an object that
            ' contains several pieces of data, or you can pass any 
            ' object or value type. The following code passes the
            ' integer value 42.
            '
            newThread.Start(42)        ' To start a thread using an instance method for the thread 
            ' procedure, use the instance variable and method name when 
            ' you create the ParameterizedThreadStart delegate. Visual 
            ' Basic expands the AddressOf expression to the appropriate 
            ' delegate creation syntax:
            '    New ParameterizedThreadStart(AddressOf w.DoMoreWork)
            '
            Dim w As New Work()
            newThread = New Thread(New ParameterizedThreadStart(AddressOf w.DoMoreWork))
            'newThread = New Thread(AddressOf w.DoMoreWork)
            
            ' Pass an object containing data for the thread.
            '
            newThread.Start("The answer.")
        End Sub
     
        Public Shared Sub DoWork(ByVal data As Object)
            Console.WriteLine("Static thread procedure. Data='{0}'", _
                data)
        End Sub    Public Sub DoMoreWork(ByVal data As Object) 
            Console.WriteLine("Instance thread procedure. Data='{0}'", _
                data)
        End Sub
    End Class' This code example produces the following output (the order 
    '   of the lines might vary):
    '
    'Static thread procedure. Data='42'
    'Instance thread procedure. Data='The answer' 
    C#  复制代码 
    using System;
    using System.Threading;public class Work
    {
        public static void Main()
        {
            // To start a thread using a shared thread procedure, use
            // the class name and method name when you create the 
            // ParameterizedThreadStart delegate.
            //
            Thread newThread = new Thread(
                new ParameterizedThreadStart(Work.DoWork));
            
            // Use the overload of the Start method that has a
            // parameter of type Object. You can create an object that
            // contains several pieces of data, or you can pass any 
            // reference type or value type. The following code passes
            // the integer value 42.
            //
            newThread.Start(42);        // To start a thread using an instance method for the thread 
            // procedure, use the instance variable and method name when 
            // you create the ParameterizedThreadStart delegate.
            //
            Work w = new Work();
            newThread = new Thread(
                new ParameterizedThreadStart(w.DoMoreWork));
            
            // Pass an object containing data for the thread.
            //
            newThread.Start("The answer.");
        }
     
        public static void DoWork(object data)
        {
            Console.WriteLine("Static thread procedure. Data='{0}'",
                data);
        }    public void DoMoreWork(object data)
        {
            Console.WriteLine("Instance thread procedure. Data='{0}'",
                data);
        }
    }/* This code example produces the following output (the order 
       of the lines might vary):Static thread procedure. Data='42'
    Instance thread procedure. Data='The answer'
    */ 
      

  4.   

    改成这样好像可以
                while (true)
                {
                    Application.DoEvents();
                    Thread.Sleep(10);
                } 但是我不明白,为什么线程里面Thread.Sleep(10);为什么还会让界面停滞?
      

  5.   

    Thread.Sleep(10); 改大点,100以上最好,太少的话,你是感觉不出来的,跟锁死的情况差不多,效果不明显
      

  6.   

    再大我都试过,就算while里没有语句也停滞
    哪些朋友实际试一下,帮一下我呀,,就这么点代码
      

  7.   

    while (true) 

         Thread.Sleep(1); 

    占用了UI线程,而且一直都不退出,当前会假死啦,Invoke相当于在UI线程里执行,你这个循环不退出,UI线程的其它语句怎么执行。
      

  8.   

    while (true) 

        Thread.Sleep(1); 
    }1太小了,这样这个操作把所有的CPU时间都吃了,让别人吃什么去阿(死机)。
    改称1000ms轮训一次把
      

  9.   

    private Thread timerThread;
            int count, i = 0;
            public void AddData()
            {
                if (i == count)
                    i = 0;
               this.lblResult.Text = i.ToString() + "/" + count.ToString();
               i++;
            }
            public void UpdataThread()
            {
                try
                {
                    MethodInvoker mi = new MethodInvoker(this.AddData);
                    while (true)
                    {
                        this.BeginInvoke(mi);
                        Thread.Sleep(50);
                    }
                }
                catch (ThreadInterruptedException)
                {
                }
                finally
                {
                }
            }
            public void StartThread()
            {
                StopThread();
                timerThread = new Thread(new ThreadStart(UpdataThread));
                  timerThread.IsBackground = true;
                timerThread.Start();
            }
            public void StopThread()
            {
                if (timerThread != null)
                {
                      timerThread.Interrupt();
                    timerThread = null;
                }
            }
            private void btnStart_Click(object sender, EventArgs e)
            {
                count = int.Parse(textBox1.Text);
                this.StartThread();
            }
            private void btnStop_Click(object sender, EventArgs e)
            {
                this.StopThread();
            }       
        }
      

  10.   

    由于lz都没理解Control.BeginInvoke的意思,所以白玩了一圈线程
      

  11.   

    用BeginInvoke调用AlarmThreadEvent,实际上是让窗口线程来调用AlarmThreadEvent,你在AlarmThreadEvent中执行死循环(不是死锁),函数永远不会返回,所以无法正常运行。在循环中加入DoEvents,可以让窗口线程在循环过程中处理事件,使窗口有响应,但还不是正常运行,因为程序始终不会从该函数返回,线程也一直处于等待状态。
      

  12.   

    用异步线程, 把GUI线程和运算线程分开.
      

  13.   

    哥们你说得太好了,我改了一下
            private void button1_Click(object sender, EventArgs e)
            {
                        AlarmThread = new Thread(new ThreadStart(AlarmThreadEventA));
                        AlarmThread.IsBackground = true;
                        AlarmThread.Start(); 
            }         private void AlarmThreadEvent()
            {
                while (true)
                {
                    AlarmThreadEventdelegate a = new AlarmThreadEventdelegate(AlarmThreadEventA);
                    this.BeginInvoke(a, new object[] { });
                    Thread.Sleep(30000);
                }
            }        public delegate void AlarmThreadEventdelegate();
            private void AlarmThreadEventA()
            {
                //大量数据处理过程
            }        改成这样不会再出现停滞情况,但是按你说的,方法AlarmThreadEventA()还是被调回主线程执行了,对吗?那是不是线程就没起作用了?请帮帮改改,应该怎么样实现比较好?我想实现的效果就是在线程里30S处理一次数据,即执行AlarmThreadEventA()