因为这个问题已经搞了一天了。由于子线程execute内容太少。自动停止。导致在vcl中调用线程终止时出错。正确方法是自动停止时等待。或者用任务死循环子线程 。
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
pcapthread* pcap;
int runflag=0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
pcap = new pcapthread(true);
pcap->Resume();
Button1->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Button2->Caption="run";
pcap->Suspend();
pcap->Terminate();
Button1->Enabled=true;
}
//---------------------------------------------------------------------------
同理,也要防止多次点击开始创建同一个线程多次。这样会导致不可预料的后果。
本文探讨了在VCL框架下如何正确管理和操作线程,特别是针对子线程自动停止的问题提出了解决方案,包括使用等待机制或任务死循环等方法,并提醒开发者注意避免重复创建线程所导致的问题。
6143

被折叠的 条评论
为什么被折叠?



