Delphi:线程的可变数组未执行数组的所有索引

时间:2018-09-19 08:35:40

标签: delphi pascal

我创建了一个线程来执行对该表的记录,然后使用循环进行了调用,如以下代码所示。这是100个循环的问题,并非所有变量I值都输入到表中。

{ TDBThread }

    procedure TDBThread.Execute;
    begin
      InsertDB(Fid);
    end;

    procedure TDBThread.InsertDB(Aid: Integer);
    var
      oConn: TFDConnection;
      oQry: TFDQuery;
    begin
      oConn := TFDConnection.Create(nil);
      oConn.ConnectionDefName := 'smen';
      oQry := TFDQuery.Create(nil);
      oQry.Connection := oConn;
      try
        try
          oQry.SQL.Text := 'insert into thethread (id,msg) values (:id,''Thread'')';
          oQry.ParamByName('id').AsInteger := Aid;
          oQry.ExecSQL;
        except
        end;
      finally
        oConn.Close;
        oQry.Connection := nil;
        oQry.Free;
        oConn.Free;
      end;
    end;

procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
      dbThread: array of TDBThread;
    begin
      SetLength(dbThread,100);
      for I := 0 to 99 do
      begin
        Application.ProcessMessages;
        dbThread[I] := TDBThread.Create(True);
        dbThread[I].FreeOnTerminate := True;
        dbThread[I].id := I+1;
        dbThread[I].Start;
        Sleep(1);
      end;
    end;

0 个答案:

没有答案