jeudi 22 mai 2014

Multithreading - CreateEvent a échoué lorsqu'il est utilisé dans un thread (Delphi XE +) - Stack Overflow


I create a Kernel-Mode driver and do something in that. And I have an User-Mode app written in Delphi called MyApp, for communication with the driver and MyApp.


I have to use Event sharing method between Driver and MyApp.


I am using a thread for doing this work. In the constructor of my thread I create a User-Mode event using CreateEvent:


...
var xxx:THandle;
....
xxx:=CreateEvent(nil, False, False, nil);

And pass it to my driver and validate that.


But when this event is being created the handle of that is not valid, and when I try to use that nothing happens even any error or exception:


procedure TMyThread.Execute;
begin
while Active do
begin
if WaitForSingleObject(xxx, INFINITE) <> WAIT_FAILED then
begin
Synchronize(DoSomething)
end
else
begin
RaiseLastOSError;
Break;
end;
Sleep(1);
end;
end;

The driver create an event and pass that by the handle of this User-Mode event handle (xxx) to MyApp.




But when I create this event in main thread: For example:


procedure TfrmMain.FormCreate(Sender: TObject);
Var
xxx::THandle;
begin
xxx:=CreateEvent(nil, False, False, nil);
end;

The handle of event (xxx) is a valid handle !!!


Edit:


When I use Delphi 7 the xxx handle is valid! Only in XE+ it is not valid. Edit my code!


Notice that I have to use threading and I have to create event in MyThread. Any idea?


(Excuse me if my English is bad)



I create a Kernel-Mode driver and do something in that. And I have an User-Mode app written in Delphi called MyApp, for communication with the driver and MyApp.


I have to use Event sharing method between Driver and MyApp.


I am using a thread for doing this work. In the constructor of my thread I create a User-Mode event using CreateEvent:


...
var xxx:THandle;
....
xxx:=CreateEvent(nil, False, False, nil);

And pass it to my driver and validate that.


But when this event is being created the handle of that is not valid, and when I try to use that nothing happens even any error or exception:


procedure TMyThread.Execute;
begin
while Active do
begin
if WaitForSingleObject(xxx, INFINITE) <> WAIT_FAILED then
begin
Synchronize(DoSomething)
end
else
begin
RaiseLastOSError;
Break;
end;
Sleep(1);
end;
end;

The driver create an event and pass that by the handle of this User-Mode event handle (xxx) to MyApp.




But when I create this event in main thread: For example:


procedure TfrmMain.FormCreate(Sender: TObject);
Var
xxx::THandle;
begin
xxx:=CreateEvent(nil, False, False, nil);
end;

The handle of event (xxx) is a valid handle !!!


Edit:


When I use Delphi 7 the xxx handle is valid! Only in XE+ it is not valid. Edit my code!


Notice that I have to use threading and I have to create event in MyThread. Any idea?


(Excuse me if my English is bad)


0 commentaires:

Enregistrer un commentaire