Einzelnen Beitrag anzeigen

mumu

Registriert seit: 28. Okt 2003
Ort: Bamberg
519 Beiträge
 
#1

Thread Programmierung für Downloadmanager

  Alt 3. Sep 2004, 20:47
hi leute! ich bin zur zeit dabei eine art downloadmanager zu programmieren. dabei verwende ich die TidHttp komponente. ich bekomme aber immer die meldunge ungültiger fensterhandle, obwohl ich alle zugriffe mit criticalsections absichere.

hier einmal der code für einen downloadthread. ich starte in meinem programm bis zu 20 threads, welche die function download aufrufen.

Delphi-Quellcode:
function Download(p: Pointer): Integer;
var parameter: PDownloadParameter;
    weiteredownloads: boolean;
    url: string;
    ordner: string;
    responsestream: TFileStream;
    filename: string;
    doppelter_filename: string;
    doppelt: integer;
    notexists: boolean;
begin
  try
      Parameter := P;
      InterlockedExchangeAdd(Anzahl_der_Threads, 1);

      try
        weiteredownloads := true;
        while (weiteredownloads = true) do
        begin

          try
            // in CriticalSection eintreten, filelist sperren
            EnterCriticalSection(g_cs_gesamt);
              if (filelist.files.Count > 0) and
              (filelist.ordner.Count > 0) and
              (form1.Listview3.Items.Count > 0) then
              begin
                url := filelist.files.strings[0];
                filelist.files.Delete(0);
                ordner := filelist.ordner.strings[0];
                filelist.ordner.Delete(0);
                form1.Listview3.Items.delete(0);
                application.ProcessMessages;
              end else
              begin
                weiteredownloads := false;
              end;
            LeaveCriticalSection(g_cs_gesamt);

            // CriticalSection verlassen
          except
            weiteredownloads := false;
          end;

          if (url = '') or (ordner = '') then
          begin
            weiteredownloads := false;
          end else
          begin
            filename := url;
            delete(filename,1,LastDelimiter('/', filename));
          end;

          EnterCriticalSection(g_cs_gesamt);
            notexists := not fileexists(unit1.currentdir+'\'+ordner+'\'+filename);
          LeaveCriticalSection(g_cs_gesamt);
          if (weiteredownloads = true) and (notexists) then
          begin
            EnterCriticalSection(g_cs_gesamt);
              Parameter.Plabel.Caption := 'Download '+inttostr(Parameter.nr)+': '+filename;
            LeaveCriticalSection(g_cs_gesamt);
            application.ProcessMessages;

            EnterCriticalSection(g_cs_gesamt);
              responseStream := nil;
            LeaveCriticalSection(g_cs_gesamt);
            try
            // in CriticalSection eintreten, filesystem sperren
              EnterCriticalSection(g_cs_gesamt);
                if not directoryexists(unit1.currentdir+'\'+ordner) then
                begin
                  MkDir(currentdir+'\'+ordner);
                end;
                if fileexists(unit1.currentdir+'\'+ordner+'\'+filename) then
                begin
                  doppelt := 0;
                  repeat
                    doppelter_filename := unit1.currentdir+'\'+ordner+'\'+inttostr(doppelt)+filename;
                    doppelt := doppelt + 1;
                  until(not fileexists(doppelter_filename));
                    responseStream := TFileStream.Create(pchar(doppelter_filename), fmCreate);
                end else
                begin
                  responseStream := TFileStream.Create(pchar(unit1.currentdir+'\'+ordner+'\'+filename), fmCreate);
                end;
              LeaveCriticalSection(g_cs_gesamt);
              // CriticalSection verlassen
            except
            end;
            try
              try
                if responseStream <> nil then
                begin
                  application.ProcessMessages;
                  Parameter.dl.Get(url, responseStream);
                  application.ProcessMessages;
                // in CriticalSection eintreten, report sperren
                  EnterCriticalSection(g_cs_gesamt);
                    Succeed(url);
                  LeaveCriticalSection(g_cs_gesamt);
                // CriticalSection verlassen
                end else
                begin
                  // in CriticalSection eintreten, report sperren
                    EnterCriticalSection(g_cs_gesamt);
                      Fail(url, unit1.currentdir+'\'+ordner+'\'+filename);
                    LeaveCriticalSection(g_cs_gesamt);
                  // CriticalSection verlassen
                end;
              except
                // in CriticalSection eintreten, report sperren
                  EnterCriticalSection(g_cs_gesamt);
                    Fail(url, unit1.currentdir+'\'+ordner+'\'+filename);
                  LeaveCriticalSection(g_cs_gesamt);
                // CriticalSection verlassen
              end;
            finally
              responseStream.free;
            end;
          end;
        end;
      finally
      end;
      EnterCriticalSection(g_cs_gesamt);
        Parameter.Plabel.Caption := 'FERTIG!';
        CloseThread();
      LeaveCriticalSection(g_cs_gesamt);
      result := 1;
  except
    Result := 2;
  end;
end;
  Mit Zitat antworten Zitat