如何让delphi等待30秒然后继续 [英] how to make delphi wait 30sec then continue

查看:894
本文介绍了如何让delphi等待30秒然后继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行无限循环,但我希望循环每30秒运行一次.循环将开始.一堆if语句发生,一些信息将被更改.然后,循环必须暂停30秒,然后循环才能再次开始.这必须永远持续下去.

I am trying to make an infinite loop but I want the loop to run every 30 seconds. The loop will start. A bunch of if statements take place and some information will be changed. The loop must then pause for 30 seconds and then the loop will start again. This must continue forever.

我正在寻找一种方法来暂停 30秒,然后继续.任何好的建议将不胜感激.

I am looking for a way to pause the loop for 30 seconds and then continue. Any good advice will be appreciated.

编辑#1

程序显示基于日期和时间的特殊"信息:随着时间的变化,信息也随之变化:06:00 =数学; 07:30 =生物学.该程序还向您显示直到下一堂课开始为止的时间.因此,该程序需要连续运行以更新时间,以便准确地知道现在是哪个时间段以及到下一个时间段还剩下多少时间.

The program shows "special" information based on date and time: As the time changes the information changes: 06:00 = math; 07:30 = biology. The program also shows you the time left until the next class starts. Thus the program needs to run continuously to update the time so that it knows exactly what period it is and how much time is left until the next period.

编辑#2

我想放入一个刷新"以便该脚本我希望以固定的时间间隔调用该脚本,以使其不会持续运行并占用公羊.此间隔必须为30秒.

I want put in a "refresh" so that script I want the script to be called on a set interval so that it is not running constantly and eating the ram. This interval must be 30 seconds.

推荐答案

如果您有阻止GUI的代码,则可以使用后台线程和事件来提供非阻止计时器.

If you have code that blocks the GUI, you can use a background thread and an event to provide a non blocking timer.

创建一个新的Forms应用程序,并在您的表单上放置一个TMemo组件. 本示例将在您的TMemo上添加一条带有当前时间的新行.

Create a new Forms application and put a TMemo component on your form. This example will add a new line with the current time to your TMemo.

主要形式:

unit u_frm_main;

interface

uses
  u_workthread,
  SysUtils,
  Windows,
  Forms,
  SyncObjs, Classes, Controls, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    Worker : TWorkThread;
    procedure ShowData;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ShowData;
begin
 // do whatever you need to do here... 
 // show current time in memo
 Memo1.Lines.Add(FormatDateTime('HH:NN:SS', Now));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 // create our worker thread and start it
 Worker := TWorkThread.Create(3, ShowData);
 Worker.Start;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 // signal our worker thread that we are done here
 Worker.ThreadEvent.SetEvent;
 // terminate and wait
 Worker.Terminate;
 Worker.WaitFor;
end;

end.

工作线程:

unit u_workthread;

interface

uses
  SysUtils,
  SyncObjs,
  Classes;

type
  TWorkProc = procedure of object;

  TWorkThread = class(TThread)
  private
    { Private declarations }
    Counter   : Integer;
    FTimeout  : Integer;
    FEventProc: TWorkProc;
    procedure DoWork;
  protected
    procedure Execute; override;
  public
    ThreadEvent : TEvent;
    constructor Create(TimeoutSeconds : Integer; EventProc: TWorkProc ); // timeout in seconds
    destructor Destroy; override;
  end;

implementation

procedure TWorkThread.DoWork;
begin
 // put your GUI blocking code in here. Make sure you never call GUI elements from this procedure
 //DoSomeLongCalculation();
end;

procedure TWorkThread.Execute;
begin
 Counter := 0;
 while not Terminated do
  begin
   if ThreadEvent.WaitFor(FTimeout) = wrTimeout then
    begin 
     DoWork;
     // now inform our main Thread that we have data
     Synchronize(FEventProc);
    end;
   else
    // ThreadEvent has been signaled, exit our loop
    Break;
  end;
end;

constructor TWorkThread.Create(TimeoutSeconds : Integer; EventProc: TWorkProc);
begin
 ThreadEvent := TEvent.Create(nil, True, False, '');
 // Convert to milliseconds
 FTimeout := TimeoutSeconds * 1000;
 FEventProc:= EventProc;
 // call inherited constructor with CreateSuspended as True
 inherited Create(True);
end;

destructor TWorkThread.Destroy;
begin
 ThreadEvent.Free;
 inherited;
end;


end.

这篇关于如何让delphi等待30秒然后继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆