Einzelnen Beitrag anzeigen

quakergod

Registriert seit: 20. Mär 2003
Ort: NRW
129 Beiträge
 
Delphi 7 Enterprise
 
#1

Events und Threads in Schleife erzeugen

  Alt 11. Feb 2006, 07:04
Hallo, ich habe folgenden Code um Threads & Events zu erzeugen und sie mit Daten zu füttern:

Delphi-Quellcode:
procedure TMainform.ButtonClick(Sender: TObject);
var dw: DWORD;
  ev1, ev2: TEvent;
  t1,t2: TevtThread;
  events: array[0..1] of THandle;
  FStatus: array[0..3] of string;
  i: integer;
begin
  FDLAbort := False;
  button.Enabled := False;

  ev1 := nil;
  ev2 := nil;
  try
    FDLThreads := 2;

         // Events anlegen
    ev1 := TEvent.Create(nil, False, False, '');
    ev2 := TEvent.Create(nil, False, False, '');
         // Threads anlegen
    t1 := TEvtThread.Create(ev1);
    t2 := TEvtThread.Create(ev2);
         // Downloadschleife
    events[0] := ev1.Handle;
    events[1] := ev2.Handle;

    while FDLThreads > 0 do begin
         // warten auf Thread oder Input
      dw := MsgWaitForMultipleObjects(2, events, False, WF_TIMEOUT {INFINITE}, QS_ALLINPUT);
      case dw of
        WAIT_OBJECT_0 + 0: SetNextDLFile(t1, FStatus[0]);
        WAIT_OBJECT_0 + 1: SetNextDLFile(t2, FStatus[1]);
        WAIT_OBJECT_0 + Length(events): HandleDLUI;
        WAIT_TIMEOUT: HandleDlUI;
      else
          // Fehler
        Break;
      end;
    end;
  finally
    button.Enabled := True;
  end;
end;
der code funktioniert auch ohne Probleme, jedoch möchte ich die anzahl der threads variabel halten,
deswegen hab ich den code so umgeschrieben:

Delphi-Quellcode:
type
  TgmDownload = record
    Event: array[0..20] of TEvent;
    EventHandle: array[0..20] of THandle;
    Thread: array[0..20] of TevtThread;
  end;

var
  FgmDownload: TgmDownload;


procedure TMainform.Button2Click(Sender: TObject);
var dw: DWORD;
  FStatus: array[0..3] of string;
  i: integer;
begin
  FDLAbort := False;
  button2.Enabled := False;

  FDLThreads := strtoint(edtDLThreads.Text);
  try
    for i := 1 to FDLThreads - 1 do
    begin
      FgmDownload.Event[i] := nil;
      FgmDownload.Event[i] := TEvent.Create(nil, False, False, '');
      FgmDownload.Thread[i] := TEvtThread.Create(FgmDownload.Event[i]);
      FgmDownload.EventHandle[i] := FgmDownload.Event[i].Handle;
    end;
   
    while FDLThreads > 0 do begin
         // warten auf Thread oder Input
      dw := MsgWaitForMultipleObjects(2, FgmDownload.EventHandle, False, WF_TIMEOUT {INFINITE}, QS_ALLINPUT);
      case dw of
        WAIT_OBJECT_0 + 0: SetNextDLFile(FgmDownload.Thread[0], FStatus[0]);
        WAIT_OBJECT_0 + 1: SetNextDLFile(FgmDownload.Thread[1], FStatus[0]);
        WAIT_OBJECT_0 + 2: SetNextDLFile(FgmDownload.Thread[2], FStatus[0]);
        WAIT_OBJECT_0 + 3: SetNextDLFile(FgmDownload.Thread[3], FStatus[0]);
        WAIT_OBJECT_0 + 4: SetNextDLFile(FgmDownload.Thread[4], FStatus[0]);
        WAIT_OBJECT_0 + 5: SetNextDLFile(FgmDownload.Thread[5], FStatus[0]);
        WAIT_OBJECT_0 + 6: SetNextDLFile(FgmDownload.Thread[6], FStatus[0]);
        WAIT_OBJECT_0 + 7: SetNextDLFile(FgmDownload.Thread[7], FStatus[0]);
        WAIT_OBJECT_0 + 8: SetNextDLFile(FgmDownload.Thread[8], FStatus[0]);
        WAIT_OBJECT_0 + 9: SetNextDLFile(FgmDownload.Thread[9], FStatus[0]);
        WAIT_OBJECT_0 + Length(FgmDownload.EventHandle): HandleDLUI;
        WAIT_TIMEOUT: HandleDlUI;
      else
         // Fehler
        Break;
      end;
    end;
  finally
    button2.Enabled := True;
  end;
end;
ich sehe leider keinen fehler, aber es kommt nach ein par sekunden zu einer nichtssagende Speicherexception ?!

Weiss jemand was ich falsch gemacht habe oder wo der Fehler liegt? (weiss nicht mehr weiter)

Gruß
I love Delphi-Praxis !
  Mit Zitat antworten Zitat