谁有TThread使用的例子

微星1234 2004-11-11 03:33:12
谁有TThread使用的例子,100分相送.
发到我的邮箱jhandpt@sohu.com
...全文
177 4 打赏 收藏 转发到动态 举报
写回复
用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

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧