验证Event的全局性

思路很简单, 创建一事件,再创建一线程等待事件,再用OpenEvent根据事件名称打开,

MSDN说明:

Fast user switching is implemented using Terminal Services sessions. The first user to log on uses session 0, the next user to log on uses session 1, and so on. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.

看得出事件具有传递性

 

代码
unit  Unit1;

interface

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

type
  TestThread 
=   class (TThread)
  
private
    I: Integer;
    
procedure  ShowMsg;
  
protected
    
procedure  Execute;  override ;
  
public
  
end ;

  TForm1 
=   class (TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    
procedure  Button1Click(Sender: TObject);
    
procedure  Button2Click(Sender: TObject);
    
procedure  Button3Click(Sender: TObject);
    
procedure  Button4Click(Sender: TObject);
    
procedure  Button5Click(Sender: TObject);
    
procedure  Button6Click(Sender: TObject);
    
procedure  Button7Click(Sender: TObject);
  
private
    
{  Private declarations  }
  
public
    
{  Public declarations  }
  
end ;

var
  Form1: TForm1;
  GHandle, GHandle1: THandle;

const
  Str: 
string   =   ' 123 ' ;

implementation

{ $R *.dfm }

procedure  TForm1.Button1Click(Sender: TObject);
begin
  GHandle :
=  CreateEvent( nil , True, False, PChar(Str));
  Memo1.Lines.Add(IntToStr(GHandle));
end ;

procedure  TForm1.Button2Click(Sender: TObject);
begin
  
try
    GHandle1 :
=  OpenEvent(EVENT_ALL_ACCESS, False, PChar(Str));
  
finally
    Memo1.Lines.Add(SysErrorMessage(GetLastError));
  
end ;
  Memo1.Lines.Add(IntToStr(GHandle1));
end ;

{  TestThread  }

procedure  TestThread.Execute;
begin
  
inherited ;
  I :
=   0 ;
  Self.FreeOnTerminate :
=  False;
  
while   not  self.Terminated  do
  
begin
    WaitForSingleObject(GHandle, INFINITE);
    Synchronize(ShowMsg);
    InterlockedIncrement(I);
  
end ;
end ;

procedure  TestThread.ShowMsg;
begin
  Form1.Memo1.Lines.Add(IntToStr(I));
end ;

procedure  TForm1.Button3Click(Sender: TObject);
var
  T: TestThread;
begin
  T :
=  TestThread.Create(False);
end ;

procedure  TForm1.Button4Click(Sender: TObject);
begin
  SetEvent(GHandle);
end ;

procedure  TForm1.Button5Click(Sender: TObject);
begin
  ResetEvent(GHandle);
end ;

procedure  TForm1.Button6Click(Sender: TObject);
begin
  SetEvent(GHandle1);
end ;

procedure  TForm1.Button7Click(Sender: TObject);
begin
  ResetEvent(GHandle1)
end ;

end .

 

 

你可能感兴趣的:(event)