Recent

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

musicones

  • New Member
  • *
  • Posts: 15
Hi,

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

Problem...

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
    begin
      while not FTerminateRequest do
      begin
        AOutput := EmptyStr;
     
        repeat
          WasOK := ReadFile(vStdOutPipe.Output, vBuffer^, READ_BLOCKSIZE, vReadBytes, nil);
     
          if (vReadBytes > 0) then
          begin
            AOutput := AOutput + StrPas(vBuffer);
     
            FireOutput(FName, pstRunning, AOutput);
          end;
     
          WaitForSingleObject(vProcessInfo.hProcess, 100);
        until not WasOK or (vReadBytes < READ_BLOCKSIZE);
      end;
    end;
         


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.

regards
musicones

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