1. 程式人生 > >C# 執行緒 正確使用Thread.Join()停止方式

C# 執行緒 正確使用Thread.Join()停止方式

/// <summary>
        /// 停下執行緒
        /// </summary>
        private void MyStopTask()
        {
            new Action(() => {
                if (thread != null)
                {
                    while (thread.ThreadState != System.Threading.ThreadState.Stopped)//必須等執行緒完全停止了,否則會出現衝突。  
                    {
                        try
                        {
                            buttonFun("登 錄");
                            thread.Join();
                            Thread.Sleep(2000);
                         
                        }
                        catch (Exception ex)
                        {
                           
                        }
                    }

                }
            }).BeginInvoke(null, null);
           
        }