#ifndef NyUnit7H
#define NyUnit7H
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class MyThread : public TThread
{
private:
protected:
void __fastcall Execute();
public:
bool Running; // 是否在运行
void (*UserProc)();
__fastcall MyThread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
#include <vcl.h>
#pragma hdrstop#include "NyUnit7.h"
#pragma package(smart_init)__fastcall MyThread::MyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall MyThread::Execute()
{
Running=true; // 运行
UserProc(); // 执行
Running=false; // 结束
}
//---------------------------------------------------------------------------
以上是线程内容
回复人:BCB(:)) (2001-6-8 12:33:00) 得0分
bool StopThread(void *handle) // 强行中止一个线程,强行成功返true
{
bool success=false;
unsigned long ExitCode;
if (GetExitCodeThread(handle,&ExitCode))
if (TerminateThread(handle,ExitCode))
success=true;
return(success);
}
TThreadList *ThreadList=NULL;
bool RunInThread(void (*userproc)(),unsigned int msec)
{ // 规定msec毫秒内执行用户提交的子程序,若正常结束返回true
MyThread *thread0=new MyThread(true);
thread0->Priority =tpHigher;
thread0->Running=true;
thread0->UserProc=userproc;
thread0->Resume(); // 线程运行了
bool NormalEnd=true;
unsigned int tick0=GetTickCount();
while (thread0->Running)
{
if (GetTickCount()-tick0>msec)
{ // 超时了,强行中止
StopThread((void *)thread0->Handle);
NormalEnd=false;
break;
}
Application->ProcessMessages();
}
if (NormalEnd)
delete thread0;
else
{
if (ThreadList==NULL)
ThreadList=new TThreadList;
ThreadList->Add((TThread *)thread0);
} // 不删了,否则时间等待太长
return(NormalEnd);
}
杀线程部分
#define NyUnit7H
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class MyThread : public TThread
{
private:
protected:
void __fastcall Execute();
public:
bool Running; // 是否在运行
void (*UserProc)();
__fastcall MyThread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
#include <vcl.h>
#pragma hdrstop#include "NyUnit7.h"
#pragma package(smart_init)__fastcall MyThread::MyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall MyThread::Execute()
{
Running=true; // 运行
UserProc(); // 执行
Running=false; // 结束
}
//---------------------------------------------------------------------------
以上是线程内容
回复人:BCB(:)) (2001-6-8 12:33:00) 得0分
bool StopThread(void *handle) // 强行中止一个线程,强行成功返true
{
bool success=false;
unsigned long ExitCode;
if (GetExitCodeThread(handle,&ExitCode))
if (TerminateThread(handle,ExitCode))
success=true;
return(success);
}
TThreadList *ThreadList=NULL;
bool RunInThread(void (*userproc)(),unsigned int msec)
{ // 规定msec毫秒内执行用户提交的子程序,若正常结束返回true
MyThread *thread0=new MyThread(true);
thread0->Priority =tpHigher;
thread0->Running=true;
thread0->UserProc=userproc;
thread0->Resume(); // 线程运行了
bool NormalEnd=true;
unsigned int tick0=GetTickCount();
while (thread0->Running)
{
if (GetTickCount()-tick0>msec)
{ // 超时了,强行中止
StopThread((void *)thread0->Handle);
NormalEnd=false;
break;
}
Application->ProcessMessages();
}
if (NormalEnd)
delete thread0;
else
{
if (ThreadList==NULL)
ThreadList=new TThreadList;
ThreadList->Add((TThread *)thread0);
} // 不删了,否则时间等待太长
return(NormalEnd);
}
杀线程部分
解决方案 »
- Indy 10.5.7版本下IdFTP.List的问题
- 从数据库image字段中的数据怎么输入到excel中
- 如果宝蓝把戴妃卖给古狗
- 怎么发送数据到指定的服务器?
- 如何ultraedit 中支持delphi的语法??
- 如何刷新系统时间
- 简单问题,来者有分,关于DataTimePicker控件和查询的问题
- 面向对象编程学习心得:抽象提高可维护性,实例来证明(delphi)
- 新手上路,但是也许高手做不了!进来看看就知道啦,分数不够可以再加!
- 小菜的多表关联问题.......
- 怎样制作多媒体音量动态显示屏,象winmp3那样,随音量的高低而动态显示!
- bcb你好!我在调用线程的时提示“fffffffff"等一串的错误,我不太熟,你能给我一个你的程序吗?可以的话,email:[email protected],谢谢!!!
针对一个无参数函数或过程,向线程提交执行,
若不能在规定的时间内执行完毕,就被强行中止