?? unitcreateevent.pas
字號:
unit UnitCreateEvent;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label1: TLabel;
Button5: TButton;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
EventHandle: THandle; // holds the event handle
ThreadHandle: THandle; // holds the thread handle
implementation
{$R *.DFM}
function ThreadFunction(Info: Pointer): Integer; stdcall;
var
FormDC: HDC; // holds a handle to the form device context
Counter: Integer; // general loop counter
CounterStr: string; // a string representation of the loop counter
ObjRtn: Integer; // wait function return value
begin
{WaitForSingleObject will wait for the event to
become signaled (ready to do something)}
ObjRtn := WaitForSingleObject(EventHandle, INFINITE);
{retrieve a handle to the form's device context}
FormDC := GetDC(Form1.Handle);
{begin a large loop}
for Counter := 1 to 100000 do
begin
{display the counter value}
CounterStr := IntToStr(Counter);
TextOut(FormDC, 10, 10, PChar(CounterStr), Length(CounterStr));
{process any pending messages}
Application.ProcessMessages;
{this causes the loop to pause, as the PulseEvent function
rapidly sets the event's signaled state to signaled and
then unsignaled}
ObjRtn := WaitForSingleObject(EventHandle, INFINITE);
end;
{release the form's device context and exit the thread}
ReleaseDC(Form1.Handle, FormDC);
ExitThread(4);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ThreadID: DWORD; // holds the thread identifier
begin
{create a new thread}
ThreadHandle := CreateThread(nil, 0, @ThreadFunction, nil, 0, ThreadId);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
{indicate that the event is signaled. this will cause the waiting
thread to get past the WaitForSingleObject function, thus starting
the loop}
SetEvent(EventHandle);
Label1.Caption := 'Event is signaled';
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
{reset the event object to a non signaled state. this will
cause the thread loop to pause at the WaitForSingleObject
function inside the loop}
ResetEvent(EventHandle);
Label1.Caption := 'Event is non signaled';
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
{if the event has been reset (above), the thread's loop will be
paused at the internal WaitForSingleObject function. PulseEvent
will toggle the event's state from nonsignaled to signaled and back,
causing the thread's loop to fire once.}
PulseEvent(EventHandle); //Set to signaled and then nonsignaled
Label1.Caption := 'signaled/nonsignaled';
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
{create the event}
EventHandle := CreateEvent(Nil, True, False, 'MyEvent');
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -