Author Topic: [solved:] ReadFile blocks. Exampler for asynchronous Readfile?  (Read 5455 times)


  • New Member
  • *
  • Posts: 15

i monitor the Output of an commandline tool with an process (created with CreateProcess).
The tool gives back an output on certain user interactions.
This means, that my process lasts forever, until I tell it to terminate.
To keep my Gui application responsible, i have implemented this all in an own thread.

Since here all ok.
1. Process starts
2. Process gives back as much output as requested
3. My Gui application keeps responsible


I use ReadFile inside a loop in the Execute of the Process. The Problem is, that ReadFile has not timeout and blocks until it gets data, then the rest of the loop will be executed, and the waiting for new data restarts.

My Problem is, i cannot stop the process while/as ReadFile is blocking.

Here some code:

Code: [Select]
    if (FWait) then
      while not FTerminateRequest do
        AOutput := EmptyStr;
          WasOK := ReadFile(vStdOutPipe.Output, vBuffer^, READ_BLOCKSIZE, vReadBytes, nil);
          if (vReadBytes > 0) then
            AOutput := AOutput + StrPas(vBuffer);
            FireOutput(FName, pstRunning, AOutput);
          WaitForSingleObject(vProcessInfo.hProcess, 100);
        until not WasOK or (vReadBytes < READ_BLOCKSIZE);

The problem is known and as solution it is said to use readfile asynchronous.
Unfortunately, I have no idea what needs to be changed to make it run asynchronous.

If someone had a small example of an asynchronous ReadFile I would be very grateful.


What I found out after posting:

Found these Sources that helped me:

I haven't got it working yet, but the sources help to understand.
First, for asynchronism I need NamedPipes, anonymous pipes do not work.
Second OVERLAPPED as FILE_FLAG_OVERLAPPED param flag for CreateNamedPipe
And then more understanding on how thing in Threading and using pipes work...
« Last Edit: August 01, 2014, 10:26:06 pm by musicones »


TinyPortal © 2005-2018