Overbyte NamedPipes; Send msg on a different thread

313 views Asked by At

I'm fairly new to IPC(interprocess communication). Doing my research, I decided on the Named pipes.
My application consists of 2 parts: a monitoring app, and a UI dashboard. The dashboard receives updates from the monitor constantly and shows stats, the user should be able to change certain parameters of the monitor through the dashboard (refresh rate, restart process,scheduled task...) so it has to be a bidirectional communication. The Monitor would later become a service application, but that's a plan for later.
Getting to the point, I used the Pipes unit by Overbyte(Francois Piette) v1.01 and followed the example made on their website. When connecting the pipes on the main thread, it functions normally. But due to my monitor needing a separate thread to (monitor-send message-sleep-loop...), when I try to connect on the other thread, I get an error "The notify window and the component window do not exist in the same thread!"

  • What I need to know is, can named pipes communicate using a separate thread? (Judging by the error msg, I feel it may be fetching the window handle on the main UI and would not work on a different thread)
  • Is there a better way to implement my application? or named pipes better than Overbyte?

Sample of my code:

TThreadMonitor = Class(TThread)
  private
    PipeClient1: TPipeClient;
    listOfProcesses: Array of String;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: Boolean);

    procedure ConnectPipe;
    procedure SendMessage(const Msg: String);
  End;

var
t: TThreadMonitor;

procedure TClientFormMain.BtnDifferentThreadClick(Sender: TObject);
begin
  t := TThreadMonitor.Create(TRUE);
  t.FreeOnTerminate := TRUE;

  t.Start;
end;

procedure TThreadMonitor.ConnectPipe;
begin
  if not PipeClient1.Connect(2000, TRUE) then
    LogThis('Pipe connection failed', LogFilePath, TRUE)
  else
    LogThis('Pipe connected', LogFilePath, TRUE);
end;

procedure TThreadMonitor.Execute;
begin
  inherited;

  ConnectPipe;  //<---throws exception here although getting "Pipe Connected"
    while not Terminated do
    begin
      for i := 0 to Length(listOfProcesses) - 1 do
      begin
        ...
        MonitorProcess(listOfProcesses[i]);
        ...
      end;
      sleep(2000);
    end;
end;
0

There are 0 answers