Mailslot blocking forever

893 views Asked by At

I decided to use mailslot for IPC. On windows 8 everything works fine. But on Windows Xp I receive fine first message but then the call to ReadFile gets stuck.

Here is my test code:

procedure TForm1.Button1Click(Sender: TObject);
var
  hand : THandle;
  buf : array [0..255] of AnsiChar;
  btsRead : DWORD;
begin
  hand := CreateMailslot('\\.\mailslot\somemailslot', 255, DWORD(-1), nil);
  if hand <> INVALID_HANDLE_VALUE then
  begin
      while True do
      begin
        ReadFile(hand, buf, 255, btsRead, nil); // call gets stuck after first message
        ShowMessage(buf);
        Application.ProcessMessages;
      end;
  end;

On Windows 8 I continue to receive messages from client application, but on windows xp the call to ReadFile blocks forever waiting for a message. I tried reopening client application without success.

What I am doing wrong ?

1

There are 1 answers

1
Sebastian Z On

Use a thread for reading from the mailslot. Otherwise you'll always have the problem that you'd have to process Windows messages while you're waiting in ReadFile.

Now having said that, the calls to CreateMailslot and ReadFile look very similar to what I use (on XP and above) except that I have a larger buffer (65535).

If neither of these helps then the problem is probably somewhere else.

When you need to cancel the call to ReadFile, call CloseHandle(hand) from the main thread. That is useful when you need to exit the thread. This is another reason for using a thread for the mailslot.