本文介绍了线程丢失而无法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我的代码如下。有时候,线程丢失但线程无法启动,因为变量isStarted保持为真。

I have below code. Sometime, the thread loss but the thread cannot start due to the variable isStarted keep true.

如何检查线程以防止重复运行?或者如何检查线程丢失并重新开始??

How to check the thread to prevent duplicate run? Or How to check the thread loss and start again??

private bool isStarted = false;

private bool isStarted =false;

private void UC_Banner_VisibleChanged(object sender,EventArgs e)

   {

            if(!this.isStarted)

            {

                this.isStarted = true;

                 CallBrdMsgThread callBrdMsgThread = new CallBrdMsgThread(this.BrdMsgStart);   //线程丢失但是  this.isStarted 保持正确。

                 this.Invoke(callBrdMsgThread,null );;
            }
  }

private void UC_Banner_VisibleChanged(object sender, EventArgs e)
  {
            if (!this.isStarted)
            {
                this.isStarted = true;
                CallBrdMsgThread callBrdMsgThread = new CallBrdMsgThread(this.BrdMsgStart);   // the thread loss but  this.isStarted  keep true.
                this.Invoke(callBrdMsgThread, null);
            }
  }

推荐答案

如果CallBrdMsgThread 是从System.Threading.Thread继承的,您可以检查IsAlive属性。另一个可能性是在线程退出时重置线程中的isStarted字段。

if CallBrdMsgThread is inherrited from System.Threading.Thread you can check the IsAlive property. An Other posibility is to reset the isStarted field in the thread, when it exit.


这篇关于线程丢失而无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 22:57