社区
C++ Builder
帖子详情
谁有TThread使用的例子
微星1234
2004-11-11 03:33:12
谁有TThread使用的例子,100分相送.
发到我的邮箱jhandpt@sohu.com
...全文
214
4
打赏
收藏
谁有TThread使用的例子
谁有TThread使用的例子,100分相送. 发到我的邮箱jhandpt@sohu.com
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
constantine
2004-11-11
打赏
举报
回复
给你发了3个,你去收取吧
铖邑
2004-11-11
打赏
举报
回复
#ifndef SingleMutexH
#define SingleMutexH
class CSingleMutex
{
private:
int FInterval;
void *FHandle;
public:
CSingleMutex(void);
CSingleMutex(const char *AName);
~CSingleMutex(void){Close();}
void Close(void);
bool Create(const char *AName);
inline bool Handle(void){return FHandle;}
bool Lock(void);
bool Open(const char *AName);
inline void SetInterval(int AInt){FInterval = AInt;}
void Unlock(void);
};
#endif
铖邑
2004-11-11
打赏
举报
回复
/* 执行线程类(在ORACLE中执行入库SQL)
入口:接受出错SQL的存放路径 */
#include <stdio.h>
#include <time.h>
#include "DateTime.h"
#include <inifiles.hpp>
#include "PublicDB.h"
#include "ExecThread.h"
__fastcall TExecuteThread::TExecuteThread(const char *APath): TThread(true)
{
FCount = 0;
FTime = time(NULL);
FErrorPath.printf("%s/errsql", APath);
FLogPath.printf("%s/log", APath);
sqlPath.printf("%s/result/sql", APath);
Priority = tpNormal;
FreeOnTerminate = false;
PublicDB.DriverName = "ORACLE";
PublicDB.ServerName = getenv("ServerName");
PublicDB.UserName = getenv("UserName");
PublicDB.Password = getenv("Password");
PublicDB.setName("DBR");
PublicDB.OpenDatabase();
if(PublicDB.Connected)RecordLog("数据库初始连接成功!");
else RecordLog("数据库初始连接失败!");
FMutex.Create("itnms.dbcommit");
}
void __fastcall TExecuteThread::RecordLog(const char *Log)
{
time_t cur = time(NULL);
tm *ptm = localtime(&cur);
String temp;
temp.printf("%s/oracle-%.4d%.2d%.2d.log", FLogPath.c_str(), ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
FILE *fp = fopen(temp.c_str(), "a");
if(fp)
{
temp.printf("%.4d-%.2d-%.2d %.2d:%.2d:%.2d %s\n", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, Log);
fputs(temp.c_str(), fp);
fclose(fp);
}
}
/* 线程执行函数Execute
从队列中取出一条SQL语句执行,并将出错信息记录日志 */
void __fastcall TExecuteThread::Execute()
{
for(; !Terminated;)
{
// 尝试数据库连接,如果连接失败,线程挂起
bool State = PublicDB.Connected;
if(PublicDB.OpenDatabase())
{
if(State == false)RecordLog("数据库连接成功!");
}
else
{
RecordLog("数据库连接失败!");
Sleep(2000);
Suspend();
continue;
}
FMutex.Lock(); // 锁定互斥量
// 从列表中取得一条SQL语句
if(FList.size() == 0)
{
FMutex.Unlock(); // 解锁互斥量
Suspend();
continue;
}
list<StructSQL>::iterator BeginSQL = FList.begin();
FMutex.Unlock(); // 解锁互斥量
if(BeginSQL == NULL)continue;
const char *SQL = BeginSQL->SQL.c_str();
if(strlen(SQL))try
{
PublicDB.ExecSQL(SQL); // 尝试执行SQL语句
}
catch(Exception &exception) // 执行失败,出现异常
{
int i;
char *pchar = exception.Message.c_str();
char *pos = strstr(pchar, "ORA-"); // 定位
if(pos && sscanf(pos, "ORA-%d", &i) == 1) // 获取ORACLE错误码
{
String temp;
char StrDate[10];
if(i == 3114)
{
PublicDB.CloseDatabase(); // 数据库连接中断
RecordLog("数据库连接中断!");
Suspend(); // 线程挂起
continue;
}
DateToStr(StrDate, GetCurDate()); // 取得当前日期
// 生成出错日志文件
temp.printf("%s/%s/ORA-%.5d-%s", FErrorPath.c_str(), BeginSQL->Type.c_str(), i, StrDate);
FILE *fp = fopen(temp.c_str(), "a");
if(fp)
{
fseek(fp, 0, SEEK_END); // 移至文件结尾
if(ftell(fp) == 0) // 新建文件
{
fputs(pchar, fp); // 写入出错信息
fputc('\n', fp);
}
fputs(SQL, fp); // 写入出错SQL语句
fputc('\n', fp);
fclose(fp);
}
}
}
FMutex.Lock(); // 锁定互斥量
FList.erase(BeginSQL); // 删除执行成功的SQL语句
bool Empty = FList.size() == 0; // SQL语句列表为空
FMutex.Unlock(); // 解锁互斥量
if(Empty)Suspend(); // 列表为空时线程挂起
}
}
void __fastcall TExecuteThread::ExecSQL(const char *ASQL, const char *AType)
{
FMutex.Lock();
FList.push_back(StructSQL(ASQL, AType));
FMutex.Unlock();
Resume();
}
//---------------------------------------------------------------------------
铖邑
2004-11-11
打赏
举报
回复
//---------------------------------------------------------------------------
#ifndef ExecThreadH
#define ExecThreadH
#include <list>
#include <Classes.hpp>
#include "SingleMutex.h"
//---------------------------------------------------------------------------
using namespace std;
struct StructSQL
{
String SQL, Type;
StructSQL(const char *ASQL, const char *AType):
SQL(ASQL), Type(AType){}
};
class TExecuteThread: public TThread
{
private:
long FCount, FTime;
CSingleMutex FMutex;
String FErrorPath, FLogPath, sqlPath;
list<StructSQL> FList;
void __fastcall Execute(void);
public:
__fastcall TExecuteThread(const char *APath);
void __fastcall RecordLog(const char *Log);
void __fastcall ExecLog(void);
void __fastcall ExecSQL(const char *ASQL, const char *AType);
};
#endif
T
Thread
线程的
例子
T
Thread
线程的
例子
D:\Documents\Embarcadero\Studio\14.0\Samples\CPP\RTL\
Thread
s T
Thread
类 该线程类可以完成大多数的线程操作 T
Thread
类的属性 1.(1)FreeOnTerminate属性 当线程终止时是否自动删除线程对象.默认值为True;语法:property Fre...
delphi 多线程
看别人的博客一万次记忆效果也没那么好,还是自己动手写写吧! function Create
Thread
( lp
Thread
Attributes: Pointer; {安全设置} dwStackSize: DWORD; {堆栈大小:0是系统默认的大小} lpStartAddress: TFN
Thread
StartRoutine; {入...
BCB 多线程T
Thread
类的
使用
T
Thread
—— 线程类详解_weixin_30662011的博客-CSDN博客T
Thread
是一个抽象类,可以创建几个独立的线程。类关系 TObject在一个多线程的应用程序中创建一个T
Thread
的后子类代表一个线程。每一新子类的T
Thread
对象的实例是一个新的线程。从T
Thread
派生的多线程实例可以构成Delphi的多线程应用程序。当一个应用程序运行时,应用程序就被载入内存准备执行。此时,它成为包含一个或多个线程的进程,每个线程含有数据、代码和系统资源。线程执行...https://blog.
掌握Delphi多线程编程:最简单的示例教程
在现代软件开发中,多线程编程是提高应用性能和响应能力的重要技术之一。Delphi作为一款功能强大的集成开发环境(IDE),通过提供高效的线程管理机制,使得开发者能够方便地实现多线程应用。自定义线程类时,首先需要确定线程将要执行的任务类型,然后在子类的Execute方法中实现这些任务。Execute方法是线程的工作函数,它会在单独的线程上下文中执行。定义线程类的步骤:1. 声明你的线程类,并确保它是T
Thread
的子类。2. 重写Execute方法,用于定义线程的工作内容。
Shell爆破需要的资料[1]
惨无人道的转载开始! 现在, Delphi 的多线程已经非常易用了!先看一个非多线程的
例子
, 代码执行时不能进行其它操作(譬如拖动窗体): {自定义方法: 在窗体上绘制...} procedure MyMethod; var i: Integer; begin for i := 0 to 500000 do begin Form1.Canvas...
C++ Builder
13,871
社区成员
102,696
社区内容
发帖
与我相关
我的任务
C++ Builder
C++ Builder相关内容讨论区
复制链接
扫一扫
分享
社区描述
C++ Builder相关内容讨论区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章