Einzelnen Beitrag anzeigen

Inspur1

Registriert seit: 28. Aug 2024
10 Beiträge
 
#12

AW: Wininet asynchronous download

  Alt Gestern, 00:27
Danke fur Hilfe.
Ich habe versucht jetzt mit WriteFile async zu schreiben. Datei wird geschrieben aber wieder ist alles frozen, bis Download fertig.
Delphi-Quellcode:
function DownloadAsync(const Url, FileName: string): Boolean;
const
  BUFFER_SIZE = 1024 * 4;
var
  inet: TAsyncInet;
  host, path: string;
  buff: array[0..BUFFER_SIZE - 1] of Byte;
  currentBytes, writtenBytes: DWORD;
  hFile: THandle;
  oFile: OVERLAPPED;
  written: Boolean;
begin
  result := false;
  inet := TAsyncInet.Create;
  try
    ExtractUrlTo(Url, host, path);
    if inet.Connect('Test', host) then
      if inet.SendRequest(_GET, path, '', '') then
      begin
        writtenBytes := 0;
        hFile := CreateFile(PChar(FileName),
                            GENERIC_WRITE,
                            0,
                            nil,
                            CREATE_ALWAYS,
                            FILE_FLAG_OVERLAPPED,
                            0);

        if hFile <> INVALID_HANDLE_VALUE then
        try
          ZeroMemory(@oFile, SizeOf(OVERLAPPED));
          oFile.Offset := $FFFFFFFF;
          oFile.OffsetHigh := $FFFFFFFF;
          oFile.hEvent := CreateEvent(nil, false, false, nil);
          repeat
            ZeroMemory(@buff, BUFFER_SIZE);
            currentBytes := inet.ReadData(@buff, BUFFER_SIZE);
            buff[currentBytes] := 0;
            written := WriteFile(hFile, @buff, currentBytes, @writtenBytes, @oFile);
            if not written then
            begin
              if GetLastError = ERROR_IO_PENDING then
              begin
                if WaitForSingleObject(oFile.hEvent, 20000) = WAIT_TIMEOUT then
                  break;
              end;
            end;
          until (currentBytes = 0);
          CloseHandle(oFile.hEvent);
          result := true;
        finally
          CloseHandle(hFile);
        end;
      end;
  finally
    inet.Free;
  end;
end;
Ist das normal und ich brauche TThread fur async download?
  Mit Zitat antworten Zitat