unit uThread;
interface
uses
Classes, SysUtils, Windows;
type
MyThread =
class(TThread)
procedure DoSomething;
procedure NetSend;
private
{ Private-Deklarationen }
protected
procedure Execute;
override;
public
ServerName, MsgName, FromName, Msg: AnsiString;
end;
implementation
uses uMain;
{ Wichtig: Methoden und Eigenschaften von Objekten in visuellen Komponenten dürfen
nur in einer Methode namens Synchronize aufgerufen werden, z.B.
Synchronize(UpdateCaption);
und UpdateCaption könnte folgendermaßen aussehen:
procedure MyThread.UpdateCaption;
begin
Form1.Caption := 'Aktualisiert in einem Thread';
end; }
{ MyThread }
procedure MyThread.Execute;
begin
try
{ Thread-Code hier einfügen }
DoSomething();
except
on e:
exception do begin
// mache hier irgendetwas mit dem Fehler.
end;
end;
end;
procedure MyThread.DoSomething;
begin
Synchronize(NetSend);
end;
procedure MyThread.NetSend;
{.$DEFINE SYNCHRONOUS}
const
szService = '
\mailslot\messngr';
MaxBufLen = $700;
var
hFile: THandle;
WrittenBytes: DWORD;
{$IFNDEF SYNCHRONOUS}
ovs: OVERLAPPED;
EventName:
String;
{$ENDIF}
begin
if Length(Msg) > MaxBufLen
then
SetLength(Msg, MaxBufLen);
{$IFNDEF SYNCHRONOUS}
EventName:='
NetSendEvent_'+ServerName;
{$ENDIF}
ServerName := '
\\' + Servername + szService;
hFile := CreateFileA(
@ServerName[1], GENERIC_WRITE, FILE_SHARE_READ,
nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL
or FILE_FLAG_NO_BUFFERING
or FILE_FLAG_OVERLAPPED, 0);
if hFile <> INVALID_HANDLE_VALUE
then
try
Msg := FromName + #0 + MsgName + #0 + Msg;
{$IFNDEF SYNCHRONOUS}
ovs.hEvent := CreateEventA(
nil, True, False, @EventName[1]);
WriteFile(hFile, Pointer(Msg)^, Length(Msg), WrittenBytes, @ovs);
{$ELSE}
WriteFile(hFile, Pointer(Msg)^, Length(Msg), WrittenBytes,
nil);
{$ENDIF}
finally
{$IFNDEF SYNCHRONOUS}
if WaitForSingleObject(ovs.hEvent, INFINITE) <> WAIT_TIMEOUT
then
{$ENDIF}
CloseHandle(hFile);
end;
end;
end.