有关线程Thread里使用Timer控件!

xiangyou0017 2012-06-06 04:50:47
最近做一个有关计时器的软体,要求是通过多线程来将秒表扩充,如何才能将Timer控件加到Thread线程中?我想要利用Timer控件的OnTimer事件控制表盘上的秒针的转动!晚上回去贴代码!急!谢谢各位大侠了!
...全文
379 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangyou0017 2012-07-18
  • 打赏
  • 举报
回复
问题已解决!!嘻嘻!
xiangyou0017 2012-06-11
  • 打赏
  • 举报
回复
请妖哥,帮忙看看。写的一个有关Thread多线程中使用Timer定时的程式,写的是一个有关带表盘的计时器,调用第一个线程没什以问题,第二个线程一打开,便出现问题(每次跳两格),如何才能解决这个问题呢?还有就是如何才能停止线程?以下的代码:
Unit2:
__fastcall TTimerThread::TTimerThread(bool CreateSuspended,TImage *Aimage)
: TThread(CreateSuspended)
{
image=Aimage;
FTimer = new TTimer(NULL);
FTimer->OnTimer = OnTimer;
FTimer->Interval = 1000;
FTimer->Enabled = true;
//FreeOnTerminate = true;
}
void __fastcall TTimerThread::OnTimer(TObject *Sender)
{
AnsiString Str;
RefreshClock();
L++;
if(L==60)
{
L=0;
K++;
}
image->Canvas->Pen->Color = clWhite;
image->Canvas->MoveTo(center_x,center_y);
image->Canvas->LineTo(center_x-r3*sin(-d*L/30),center_y-r3*cos(-d*L/30));
Str = FormatDateTime("hh:mm",IntToStr(K)+":"+IntToStr(L));
}
void __fastcall TTimerThread::Execute()
{
SetName();
//---- Place thread code here ----
MSG msg;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Unit1:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(Button1->Caption == "Run")
{
thread1 = new TTimerThread(true,img_Left);
Button1->Caption="Stop";
thread1->Resume();
}
else
{
Button1->Caption = "Run";
thread1->Terminate();
thread1->WaitFor();
Application->Terminate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(Button2->Caption == "Run")
{
thread2 = new TTimerThread(true,img_Right);
Button2->Caption = "Stop";
thread2->Resume();
}
else
{
Button2->Caption = "Run";
}
}
xiangyou0017 2012-06-07
  • 打赏
  • 举报
回复
谢谢,呵呵,我去看看,首先Timer控件的OnTimer事件就不能在Thread里使用!!
fwc 2012-06-07
  • 打赏
  • 举报
回复
用SetTimer第一个参数为NULL,然后自己做回调函数,线程里还要搞个消息处理。
当然也可以第一个参数是界面线程的句柄,可以上google搜,例程很多
xiangyou0017 2012-06-07
  • 打赏
  • 举报
回复
请问能否给个在Thread中使用Timer的例子?
dataxdata 2012-06-07
  • 打赏
  • 举报
回复
可以使用API中的SetTimer函数
CCED136 2012-06-06
  • 打赏
  • 举报
回复
在线程中使用 Timer , 由于涉及到消息传递的问题, 一两句话说不清楚。 建议看看 陈宽达的 《c++builder 深度历险》 这本书中的 “定时器”章节
xiangyou0017 2012-06-06
  • 打赏
  • 举报
回复
妖哥,想通过Timer控件达到,通过Thread多线程控制!要如何才能实现呢?
我再贴下单个界面的代码:
#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

int center_x=75,center_y=75,r=75,r2=60,r3=68,r4=53,r5=30,r6=90;
float a,c;
const float d=M_PI;
int L, K;

Tfrm_Main *frm_Main;
//---------------------------------------------------------------------------
__fastcall Tfrm_Main::Tfrm_Main(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall Tfrm_Main::RefreshClock()
{
img_Left->Canvas->Pen->Width=2;
img_Left->Canvas->Pen->Color=clBlack;
img_Left->Canvas->Pen->Style=psSolid;
img_Left->Canvas->Brush->Style=bsSolid;
img_Left->Canvas->Brush->Color=clGreen;
img_Left->Canvas->Ellipse(center_x-r,center_y-r,center_x+r,center_y+r);
img_Left->Canvas->Brush->Color=clBlack;
img_Left->Canvas->Ellipse(center_x-3,center_y-3,center_x+3,center_y+3);
img_Left->Canvas->Brush->Style=bsClear;

for(c=1;c<13;c++) //画出大刻度;
{
img_Left->Canvas->MoveTo(center_x+r2*sin(d*c/6),center_y+r2*cos(d*c/6));
img_Left->Canvas->LineTo(center_x+r*sin(d*c/6),center_y+r*cos(d*c/6));
img_Left->Canvas->Font->Color = clYellow;
img_Left->Canvas->TextOut(center_x-r4*sin(-d*c/6)-6,center_y-r4*cos(-d*c/6)-6,c);
}

for(a=1;a<61;a++) //画出小刻度;
{
img_Left->Canvas->MoveTo(center_x+r3*sin(d*a/30),center_y+r3*cos(d*a/30));
img_Left->Canvas->LineTo(center_x+r*sin(d*a/30),center_y+r*cos(d*a/30));
}
}
//---------------------------------------------------------------------------
void __fastcall Tfrm_Main::btn_Run_1Click(TObject *Sender)
{
if(btn_Run_1->Caption == "Run")
{
RefreshClock();

btn_Run_1->Caption = "Stop";
K = 0;
L = 0;
img_Left->Canvas->Pen->Color=clWhite;
img_Left->Canvas->MoveTo(center_x,center_y);
img_Left->Canvas->LineTo(center_y,center_y-r3);
lb_Time_1->Caption = "00:00";
tim_Clock->Enabled = true;
}
else
{
btn_Run_1->Caption = "Run";
tim_Clock->Enabled = false;
}
}
//---------------------------------------------------------------------------
void __fastcall Tfrm_Main::tim_ClockTimer(TObject *Sender)
{
AnsiString Str;
RefreshClock();

L++;
if(L==60)
{
L = 0;
K++;
}
img_Left->Canvas->Pen->Color=clWhite; //画出秒针;
img_Left->Canvas->MoveTo(center_x,center_y);
img_Left->Canvas->LineTo(center_x-r3*sin(-d*L/30),center_y-r3*cos(-d*L/30));

Str = FormatDateTime("hh:mm",IntToStr(K)+":"+IntToStr(L));
lb_Time_1->Caption = Str;
}
//---------------------------------------------------------------------------
void __fastcall Tfrm_Main::FormCreate(TObject *Sender)
{
RefreshClock();
}
xiangyou0017 2012-06-06
  • 打赏
  • 举报
回复
//以下是Thread线程里的代码;
__fastcall TMyThread::TMyThread(bool CreateSuspended,TImage *Image)
: TThread(CreateSuspended)
{
im = Image;
}
__fastcall TMyThread::TMyThread(bool CreateSuspended,TTimer *Timer)
:TThread(CreateSuspended)
{
Timer1 = Timer;
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
refresh();
Timer();
}
//这是在Image中画出一个表盘
void __fastcall TMyThread::refresh()
{
int center_x=75,center_y=75,r=75,r2=60,r3=68,r4=53,r5=30,r6=90;//这里定义全局变量的话,表盘画不完整而且乱;不知道为什么?
float a,c;
const float d=M_PI;
im->Canvas->Pen->Width=2;
im->Canvas->Pen->Color=clBlack;
im->Canvas->Pen->Style=psSolid;
im->Canvas->Brush->Style=bsSolid;
im->Canvas->Brush->Color=clGreen;
im->Canvas->Ellipse(center_x-r,center_y-r,center_x+r,center_y+r);
im->Canvas->Brush->Color=clBlack;
im->Canvas->Ellipse(center_x-3,center_y-3,center_x+3,center_y+3);
im->Canvas->Brush->Style=bsClear;
for(c=1;c<13;c++) //画出大刻度;
{
im->Canvas->MoveTo(center_x+r2*sin(d*c/6),center_y+r2*cos(d*c/6));
im->Canvas->LineTo(center_x+r*sin(d*c/6),center_y+r*cos(d*c/6));
im->Canvas->Font->Color = clYellow;
im->Canvas->TextOut(center_x-r4*sin(-d*c/6)-6,center_y-r4*cos(-d*c/6)-6,c);
Application->ProcessMessages();
}
for(a=1;a<61;a++) //画出小刻度;
{
im->Canvas->MoveTo(center_x+r3*sin(d*a/30),center_y+r3*cos(d*a/30));
im->Canvas->LineTo(center_x+r*sin(d*a/30),center_y+r*cos(d*a/30));
Application->ProcessMessages();
}
}
mabaoyes 2012-06-06
  • 打赏
  • 举报
回复
线程中使用,sleep 做延时。
要更精确的,循环读时间,计算时间差(用百分秒)
ccrun.com 2012-06-06
  • 打赏
  • 举报
回复
不建议在Thread线程中使用Timer控件。可以考虑用多媒体定时器之类的。

13,871

社区成员

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

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