在“IsAlive”属性为false后,Join拒绝确认子线程已终止. C#
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在“IsAlive”属性为false后,Join拒绝确认子线程已终止. C#,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3598字,纯文字阅读大概需要6分钟。
内容图文
![在“IsAlive”属性为false后,Join拒绝确认子线程已终止. C#](/upload/InfoBanner/zyjiaocheng/791/5f9db371df4847a6ba1e73ca771b4482.jpg)
果壳:我从我的表单中启动一个线程,然后一段时间后使用它的Join方法.它终止但我的应用程序停留在Join上,并拒绝承认它已完成加入.什么会导致这种情况发生?我的线程是从我的表单上的一个按钮启动的,并尝试从同一表单上的第二个按钮加入.
更多信息:
我有一个使用线程来完成通信和数字运算的应用程序.假设主窗体是父线程,第一个子节点是Child1.启动时,Child1与外部设备建立一些通信,并启动自己的2个子线程(Child2和Child3)来处理传入的数据.
当用户决定应用程序要停止处理传入数据时,我需要Child1终止(因此,如果需要,可以在恢复之前更改com设置).我设置了一个停止事件,Child1退出了它的执行循环,它做的第一件事就是通知Child2和Child3不再需要它们(通过另一个停止事件),Child2中的Join方法等待Child2和Child3.这很好用.
不起作用的是,在设置提示Child1退出运行循环并终止的停止事件后,表单也在Child1上使用Join方法,但是,此Join无限期地等待.
单步执行:当我单步执行我的应用程序时,我注意到在使用Join之前,IsAlive属性是真的.在我点击Child1.Join()后,我再也无法从线程中获取任何信息,因为它位于“JoinWaitSleep”中.但是,如果我运行while循环导致表单线程在Child1.IsAlive为true时休眠,那么这很好.我的第二个按钮是否是一个无法将Child1加入它的线程的一部分?
public void Run()
{ //Known as Child1
//code to setup coms is here
//Launch Builder threads
InsertBackgroundMonitor("Launching Collector Threads");
RunBuilders = true;
//Known as Child2 and Child3
BuildThread1 = new Thread(new ThreadStart(Cam1Builder));
BuildThread2 = new Thread(new ThreadStart(Cam2Builder));
BuildThread1.Start();
BuildThread2.Start();
while (!StopEventHandle.WaitOne(0, true))
{
//// Code that waits for coms and tosses data into lists
}
RunBuilders = false;
//Wait for threads to terminate
BuildThread1.Join();
BuildThread2.Join();
}
private void RunButton_Click(object sender, System.EventArgs e)
{
//Button for running the control thread
ControlThread = new Thread(new ThreadStart(Run));
ControlThread.Start();
}
private void StopButton_Click(object sender, System.EventArgs e)
{
if (btnStop.Enabled)
{ //Button for stopping the control thread
StopEventHandle.Set();
if (ControlThread != null)
{
while (ControlThread.IsAlive)
{
Thread.Sleep(100);
}
//somehow Join did not work
//ControlThread.Join();
}
//Update buttons
btnStart.Enabled = true;
btnStop.Enabled = false;
}
}
解决方法:
如果没有更多的代码,很难说,但是你有一些资源已经启动,你没有正确关闭.
Join()等待线程完全关闭,并在返回之前释放所有资源.如果线程有任何BackgroundWorker任务,或者如果它有任何你没有显示仍在运行的备用任务,它将不会返回.
由于BuildThread1和BuildThread2都正确返回,并且正在加入,你可以确定它不是其中之一,或者它们正在做的任何事情.查看其余代码.它有什么作用?
编辑:
这很好用:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread ControlThread;
Thread BuildThread1;
Thread BuildThread2;
volatile bool RunBuilders = true;
volatile bool RunControl = true;
private void button1_Click(object sender, EventArgs e)
{
ControlThread = new Thread(new ThreadStart(Run));
ControlThread.Start();
}
private void button2_Click(object sender, EventArgs e)
{
RunControl = false;
if (ControlThread != null)
{
while (ControlThread.IsAlive)
{
Thread.Sleep(100);
}
//somehow Join did not work
ControlThread.Join();
}
}
public void Run()
{ //Known as Child1
//code to setup coms is here
//Launch Builder threads
RunBuilders = true;
//Known as Child2 and Child3
BuildThread1 = new Thread(new ThreadStart(Cam1Builder));
BuildThread2 = new Thread(new ThreadStart(Cam1Builder));
BuildThread1.Start();
BuildThread2.Start();
while (RunControl)
{
//// Code that waits for coms and tosses data into lists
}
RunBuilders = false;
//Wait for threads to terminate
BuildThread1.Join();
BuildThread2.Join();
}
public void Cam1Builder()
{
while ( RunBuilders )
{
}
}
}
}
内容总结
以上是互联网集市为您收集整理的在“IsAlive”属性为false后,Join拒绝确认子线程已终止. C#全部内容,希望文章能够帮你解决在“IsAlive”属性为false后,Join拒绝确认子线程已终止. C#所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。