delphi - 定时器进程影响delphi中的用户界面

标签 delphi timer background delphi-7

我开发了用于从读卡器读取信息的应用程序。在这里,我使用计时器每五秒获取一次信息,因此每五秒用户界面就会变慢 因为它从读者那里获取信息。如何在后台运行计时器而不影响用户界面

unit frmVistorreg;

interface

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

type
 thread1=class(TThread)
 private
 FEvent: THandle;
 protected
 procedure Execute; override;
 public
 procedure MyTerminate;
end;


TForm3 = class(TForm)     
txt_name: TEdit;   
txt_cardno.Text  TEdit;        

private

public

end;

var
Form3: TForm3;

implementation

{$R *.dfm}


procedure thread1.Execute;
var
idcard_info :array[0..1024*5] of byte;
flag :Integer;
portflag :Integer;
st :TStrings;
str :string;  
begin

FEvent:= CreateEvent(nil, False, false, nil);
try
while not Terminated do begin
if  MainForm.PortFlag=0 then
begin

 Form3.Label11.Caption:='port has been successfully opened';
 Form3.Label11.Font.Color :=32768;
 flag := GetIdCardInfo(@idcard_info[0],1024*5,5);
 str := byteArray2Str(@idcard_info[0],1024*5);

        if(flag=0) then
        begin
           st := TStringList.Create;
           try
              SplitStr('^_^',str,st);
              Form3.txt_name.Text := st.Strings[0]; 
              Form3.txt_cardno.Text := st.Strings[5]; 
          finally
        st.Free;
          end;
        end;

end
else
begin 

Form3.Label11.Caption:='Please open the port';
Form3.Label11.Font.Color:=clRed;
end;

if WaitForSingleObject(FEvent, 500) <> WAIT_TIMEOUT // 5 seconds timeout
then Terminate;
end;

finally
CloseHandle(FEvent);
end;
end;


procedure thread1.MyTerminate;
begin
 SetEvent(FEvent);
end;


procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
var
Objthread1:thread1;
begin   
Objthread1.MyTerminate;
Action := caFree;
end;


procedure TForm3.FormCreate(Sender: TObject);
var
Objthread1:thread1;
begin
Objthread1:=thread1.Create(false);  
end;

end.

当我关闭表单时出现错误,例如

Project MDIAPP.exe raised exception class EAccessViolation with message 'Access violation at address 0051B9F1 in module 'MDIAPP.exe'. Read of address 00000198'.

我该如何解决这个问题。

最佳答案

您不需要计时器组件,您需要一个后台线程。最简单的解决方案是在线程中使用 Sleep 函数:

unit Unit2;

interface

uses
  Classes;

type
  TMyThread = class(TThread)
  protected
    procedure Execute; override;
  end;

implementation

procedure TMyThread.Execute;
begin
  while not Terminated do begin
// do your processing here
    Sleep(5000);   // wait 5 seconds
  end;
end;

end.

更好的方法是使用 WaitForSingleObject 和事件而不是 Sleep 以便能够立即终止后台线程,而不会延迟 5 秒:

unit Unit2;

interface

uses
  Windows, Classes;

type
  TMyThread = class(TThread)
  private
    FEvent: THandle;
  protected
    procedure Execute; override;
  public
    procedure MyTerminate;
  end;

implementation

procedure TMyThread.Execute;
begin
  FEvent:= CreateEvent(nil, False, False, nil);
  try
    while not Terminated do begin
// do your processing here
// ..
      if WaitForSingleObject(FEvent, 5000) <> WAIT_TIMEOUT // 5 seconds timeout
        then Terminate;
    end;
  finally
    CloseHandle(FEvent);
  end;
end;

procedure TMyThread.MyTerminate;
begin
  SetEvent(FEvent);
end;

end.

要在关闭表单时终止 TMyThread 实例,请调用表单的 OnClose 事件处理程序中的 MyTerminate 方法。

是的,了解您收到的错误消息很有趣,而不仅仅是“显示错误”。

关于delphi - 定时器进程影响delphi中的用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11218868/

相关文章:

json - 如何使用 X-SuperObject 访问 JSON 中的嵌套值

java - 如何将 java.lang.String 转换为 java.util.Timer?

JavaScript:允许用户更改背景图像

delphi - Delphi从表单应用程序运行控制台应用程序

delphi - 需要一个允许我使用 RSA 加密/解密某些文本的 Delphi 组件/库

delphi - TPNGImage.LoadFromFile EPNGInvalidFileHeader

javascript - 创建 24 小时倒计时并在到达一天中的特定时间后重置的最佳方法是什么?

c# - Xamarin.Android 更新 UI 的定时器 - runOnUiThread

html - CSS 不显示背景(溢出 :auto not helping)

PHP:即使使用 ">/dev/null 2>&1 &",exec() 也不会在后台运行