定时器问题.(在有窗体程序正常,控制台下不运行!) ( 积分: 100 )

  • 主题发起人 主题发起人 32881
  • 开始时间 开始时间
3

32881

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
var
hTimer: LongWord=0;

// 删除定时器
procedure DownTimerOff;
begin
if (hTimer <> 0) then
begin
KillTimer(0, hTimer);
hTimer := 0;
end;
end;
procedure TimerProc();
begin
DownTimerOff;
MessageBox(0, '定时器执行成功!!', '提示', mb_iconinformation);
end;
// 安装定时器
procedure DownTimerOn;
begin
if (hTimer <> 0) then DownTimerOff;
hTimer := SetTimer(0, 0,5000, @TimerProc);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
DownTimerOn;
end;

end.

在有窗体的程序中定时器运行正常...

在无窗体的控制台程序下..

program Project2;


uses
windows;

var
hTimer: LongWord=0;

// 删除定时器
procedure DownTimerOff;
begin
if (hTimer <> 0) then
begin
KillTimer(0, hTimer);
hTimer := 0;
end;
end;
procedure TimerProc();
begin
DownTimerOff;
MessageBox(0, '定时器执行成功!!', '提示', mb_iconinformation);
end;
// 安装定时器
procedure DownTimerOn;
begin
if (hTimer <> 0) then DownTimerOff;
hTimer := SetTimer(0, 0,5000, @TimerProc);
end;

begin
DownTimerOn;
end.

为什么程序不安装定时器?如何解决这个问题?请各位前辈指教..
请说明完整,谢谢...
 
因为没有消息循环(注意是“循环”),程序就退出了。
控制台程序的写法跟窗体程序写法不一样的,不能一概而论的。
 
那应该怎么搞呢???让以上的代码可以在控制台程序下运行呢?
 
那如果是'循环'的话,是不是程序有一次循环之后就可以成功安装定时器了呢?

用消息循环的话,应该就会出现程序循环安装定时器的问题了..解决的话,好象还要使用 postmessage sendmessage 这些.很麻烦..有没别的方法激活定时器呢??
或者有谁可以将以上代码的消息循环激活给做完整呢??让程序只安装一次定时器...

小弟初学乍练,,水平有限,还望前辈大侠多多指教哈..
 
delphi深度历险里有这块的介绍.就是缺少消息循环的原因.
 
program Project2;


uses
windows;

var
hTimer: LongWord=0;
Msg: tMsg;
// 删除定时器
procedure DownTimerOff;
begin
if (hTimer <> 0) then
begin
KillTimer(0, hTimer);
hTimer := 0;
end;
end;
procedure TimerProc();
begin
//DownTimerOff;
MessageBox(0, '定时器执行成功!!', '提示', mb_iconinformation);
end;
// 安装定时器
procedure DownTimerOn;
begin
if (hTimer <> 0) then DownTimerOff;
hTimer := SetTimer(0, 0,1000, @TimerProc);
end;

begin
DownTimerOn;
while GetMessage(Msg, 0, 0, 0) do
DispatchMessage(Msg);
DownTimerOn;
end.
 
照这样的话,,会循环安装定时器,,如何让定时器只运行一次呢?
 
自己顶一下!!!!
 
没人回答吗?
 
象“总有爱”那样写就差不多了

在project文件的
begin end 之间,开始时安装定时器,然后消息循环--注意不是循环安装定时器。
不过,控制台程序的消息循环要自己手工写,比较麻烦些,最好先深入了解windows消息循环再动手。
 
那能给出例子吗?
 
赶问
控制台的时候你有接受Timer消息的句柄么?
消息循环工作正常么?
看看这个这个是SetTimer的说明----------------------------------------------------


The SetTimer function creates a timer with the specified time-out value.

UINT SetTimer(

HWND hWnd, // handle of window for timer messages
UINT nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // address of timer procedure
);


Parameters

hWnd

Identifies the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.

nIDEvent

Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored.

uElapse

Specifies the time-out value, in milliseconds.

lpTimerFunc

Points to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc.
If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message's MSG structure contains the value of the hWnd parameter.



Return Values

If the function succeeds, the return value is an integer identifying the new timer. An application can pass this value, or the string identifier, if it exists, to the KillTimer function to destroy the timer. If the function fails to create a timer, the return value is zero.

Remarks

An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the DispatchMessage function simply calls the callback function instead of the window procedure. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER.

The wParam parameter of the WM_TIMER message contains the value of the nIDEvent parameter.

如果还没有解决的话----------------------------------------------------

为什么不直接用Delphi封装好了的TTimer?
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
Timer : TTimer;
procedure TimerProc();
constructor Create(Owner: TComponent);override;
end;

Timer: TTimer;


var
Form1: TForm1;


procedure TForm1.TimerProc();
begin
Self.Timer.Enabled := False;
MessageBox(0, '定时器执行成功!!', '提示', mb_iconinformation);
end;

TForm1.Create(Owner: TComponent);
begin
Inherited;
Self.Timer := TTimer.Create(Self);
Self.Timer.OnTimer := Self.TimerProc;
//设置其他的属性
//如周期等
end;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部