社区
C++ Builder
帖子详情
有关线程Thread里使用Timer控件!
xiangyou0017
2012-06-06 04:50:47
最近做一个有关计时器的软体,要求是通过多线程来将秒表扩充,如何才能将Timer控件加到Thread线程中?我想要利用Timer控件的OnTimer事件控制表盘上的秒针的转动!晚上回去贴代码!急!谢谢各位大侠了!
...全文
447
11
打赏
收藏
有关线程Thread里使用Timer控件!
最近做一个有关计时器的软体,要求是通过多线程来将秒表扩充,如何才能将Timer控件加到Thread线程中?我想要利用Timer控件的OnTimer事件控制表盘上的秒针的转动!晚上回去贴代码!急!谢谢各位大侠了!
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
用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控件。可以考虑用多媒体定时器之类的。
深入掌握C#中的
Timer
控件
与多
线程
编程技术
UI更新选,后台任务选避免直接操作
Thread
,优先
使用
Task.Run类库中默认
使用
不要滥用.Result/.Wait()`,特别是在UI
线程
高频I/O用减小GC压力加锁遵循统一顺序,防止死锁合理
使用
实现优雅取消并发编程不是魔法,而是工程艺术。掌握这些核心机制,你就能写出既高效又稳定的C#应用。💪“真正的高手,不是靠蛮力解决问题,而是理解系统的设计哲学。” —— 某不愿透露姓名的架构师 😎本文还有配套的精品资源,点击获取。
有关
timer
控件
的
使用
说明
1.
timer
不是单独的
线程
* 2.不要让主
线程
给sleep。极易界面假死,界面变白或者未响应,僵在那
里
。 * 3.对于
控件
操作,
timer
由于 System.
Thread
ing.
Thread
,对于非
控件
操作 System.
Thread
ing.
Thread
优于
timer
关于C/S程序中
Timer
控件
中
使用
thread
.sleep方法的一点注意
在C/S程序中 如果
使用
timer
控件
在
timer
控件
的tick方法中如果
使用
了
thread
.sleep方法的话请注意一下,由于
timer
控件
每次执行tick方法的时候是创建一个
线程
来跑的,所以你的sleep其实也就是sleep当前这个
线程
,它不会让你的
timer
控件
中断计时。假设你的
timer
控件
设置了10秒执行一次tick方法。那如果你的tick方法
里
头由于有用了Thre...
C#-
Timer
定时器
使用
需要放在定时器
里
的程序比较多,常用有两种方法: 方法一,
使用
winform
里
的
timer
控件
,Tick()
里
放一个多
线程
优点:简单 缺点:占用主
线程
的资源。 方法二:直接利用
Thread
.
Timer
使用
方法: System.
Thread
ing.
Timer
thread
Timer
1;
thread
Timer
1 = new System.
Thread
ing.
Timer
(new Sys...
用
线程
实现C#中的倒计时
考试的时候,会有一个倒计时的实现。一般我们会选择
使用
Timer
控件
来实现,几句代码就可以简单的实现了。从网上找到一种方法可以通过进程睡觉来实现倒计时。感兴趣的可以研究一下,下面的两种方法: 一、普通
Timer
控件
实现倒计时: 假设倒计时60分钟,如图一个label记录总的倒计时时间。 插入一个
timer
控件
,在
timer
1_
C++ Builder
13,871
社区成员
102,696
社区内容
发帖
与我相关
我的任务
C++ Builder
C++ Builder相关内容讨论区
复制链接
扫一扫
分享
社区描述
C++ Builder相关内容讨论区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章