recthread.pas

来自「CAN测试程序。华控CAN50模块(can转usb)的测试程序(VC」· PAS 代码 · 共 126 行

PAS
126
字号
unit RecThread;

interface

uses
  Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF},StdCtrls,SyncObjs,SysUtils,Goalvar;

type
  TRecThread = class(TThread)
  private
    Rec_Str :TEdit;
    Rec_Count :TEdit;
    procedure SetName;
  protected
    procedure Execute; override;
  public
    Procedure Init(Receive_Str :TEdit;Receive_Count :TEdit);

  end;

implementation

{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TRecThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{$IFDEF MSWINDOWS}
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PChar;        // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
{$ENDIF}

{ TRecThread }

procedure TRecThread.Init(Receive_Str :TEdit;Receive_Count :TEdit);
begin
   Rec_Str := Receive_Str;
   Rec_Count := Receive_Count;
end;



procedure TRecThread.SetName;
{$IFDEF MSWINDOWS}
var
  ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
  ThreadNameInfo.FType := $1000;
  ThreadNameInfo.FName := 'RecThread';
  ThreadNameInfo.FThreadID := $FFFFFFFF;
  ThreadNameInfo.FFlags := 0;

  try
    RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
  except
  end;
{$ENDIF}
end;

procedure TRecThread.Execute;
var
  count : integer;
  sTemp : String;
begin
  SetName;
  { Place thread code here }

	while g_ReadThreadFlag = TRUE do
  begin
		if(g_RecMode=0) then//事件接收方式
		begin
			if(g_hReadEvent_port[g_RecPortNo].WaitFor(INFINITE)= wrSignaled) then
			begin
				g_hReadEvent_port[g_RecPortNo].ResetEvent();
				while(HKCanReadFrame(@g_mDevHandle,g_RecPortNo,@g_RecFrame)>=0) do
				begin
					g_RecCount :=g_RecCount+1;

					if(g_ShowProcess) then
					begin
						sTemp :='';
           for count :=2 to 9 do
             sTemp :=sTemp+ char(g_RecFrame.mFrame[count]);
           Rec_Str.Text := sTemp;
           Rec_Count.Text := inttostr(g_RecCount);
					end;
				end;
			end;

		end	else begin//轮询接收方式
			while(HKCanReadFrame(@g_mDevHandle,g_RecPortNo,@g_RecFrame)>=0) do
			begin
				g_RecCount :=g_RecCount+1;

					if(g_ShowProcess) then
					begin
						Rec_Str.Text :='';
           for count :=2 to 9 do
             Rec_Str.Text :=Rec_Str.Text+ char(g_RecFrame.mFrame[count]);
           Rec_Count.Text := inttostr(g_RecCount);

					end;
			end;

			Sleep(20);//以防止过多占用CPU的资源
		end;
   end;
	end;


end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?