Multithreading 使用Omnithread库在For循环中创建新任务

Multithreading 使用Omnithread库在For循环中创建新任务,multithreading,delphi,otl,Multithreading,Delphi,Otl,我正在编写一个程序,检测USB插入并将插入的驱动器保存到StringList中。 现在,我想为StringList的每个内容创建一个新任务或后台工作程序,立即启动它们,并使用OmniThread库等待所有任务完成 这是我的代码片段 uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Winapi.ShellApi, OtlCommon, OtlCollections, OtlParalle

我正在编写一个程序,检测USB插入并将插入的驱动器保存到StringList中。 现在,我想为StringList的每个内容创建一个新任务或后台工作程序,立即启动它们,并使用OmniThread库等待所有任务完成

这是我的代码片段

uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
  Winapi.ShellApi, OtlCommon,
  OtlCollections,
  OtlParallel, Vcl.Dialogs;

procedure TUSB.WMDeviceChange(var Msg: TMessage);
var
  lpdbhHeader: PDevBroadcastHeader;
  ResStringList, TempSenderList: TStringList;
  i: Integer;

begin
  lpdbhHeader := PDevBroadcastHeader(Msg.LParam);
  ResStringList := TStringList.Create;
  TempSenderList := TStringList.Create;
  ResStringList.Clear;
  TempSenderList.Clear;

  try
    case Msg.WParam of
      DBT_DEVICEARRIVAL:
        begin
          if (lpdbhHeader^.dbch_devicetype = DBT_DEVTYP_VOLUME) then
          begin

            Sleep(2000);
            // Procedure to get connected USB Devices and save to a StringList

            (GetDrive(PDevBroadcastVolume(Msg.LParam), ResStringList));

            for i := 0 to ResStringList.Count - 1 do
            begin
              { How do I create a NewTask or
                Background Worker for each content in ResStringList
                to execute WalkDirectory and Parallel.ForEach(0, TempSenderList.Count - 1)
                in the NewTask? }

              // Procedure to Recurse content of the USB Drive and Save to TempSenderList

              WalkDirectory(ExcludeTrailingPathDelimiter(ResStringList.Strings
                [i]), TempSenderList);

              Parallel.ForEach(0, TempSenderList.Count - 1).Execute(
                procedure(const value: Integer)
                begin

                  // DoSomeThing(TempSenderList[value]);

                end);

              ShowMessage(TempSenderList.Text);

            end;

          end;
        end;
      DBT_DEVICEREMOVECOMPLETE:
        begin
          if (lpdbhHeader^.dbch_devicetype = DBT_DEVTYP_VOLUME) then
          begin
            // ShowMessage('Removed');
          end;

        end;
    end;
  finally
    ResStringList.Free;
    TempSenderList.Free;
  end;

end;

使用Delphi XE7和OmniThread Library v3.04。

您有什么问题吗?@gabr您看到了吗?开发人员说他的(实现的)目标是超越TOmniMREW,因此如果这是真的,也许您可以使用它来增强您的性能