【更新说明】
如果我们在顺序作业执行过程中,想要临时在作业中插入一个作业时,由于 Add 函数只能追加到尾部而无法完成。新增的 Insert 函数可以辅助用户方便的完成此作业。
首先我们看函数说明:
/// <summary>插入一个作业过程,如果准备内部计数器为0,则直接执行,否则只添加到列表</summary> /// <param name="AIndex">要插入的的位置索引</param> /// <param name="AProc">要执行的作业过程</param> /// <param name="AData">附加数据指针</param> /// <param name="AInMainThread">作业是否需要在主线程中执行</param> /// <param name="AFreeType">AData指定的附加数据指针释放方式</param> /// <returns>成功,返回True,失败,返回False</returns> /// <remarks>添加到分组中的作业,要么执行完成,要么被取消,不运行通过句柄取消</remarks> function Insert(AIndex:Integer;AProc: TQJobProc; AData: Pointer; AInMainThread: Boolean = False; AFreeType: TQJobDataFreeType = jdfFreeByUser): Boolean; overload; /// <summary>插入一个作业过程,如果准备内部计数器为0,则直接执行,否则只添加到列表</summary> /// <param name="AIndex">要插入的的位置索引</param> /// <param name="AProc">要执行的作业过程</param> /// <param name="AData">附加数据指针</param> /// <param name="AInMainThread">作业是否需要在主线程中执行</param> /// <param name="AFreeType">AData指定的附加数据指针释放方式</param> /// <returns>成功,返回True,失败,返回False</returns> /// <remarks>添加到分组中的作业,要么执行完成,要么被取消,不运行通过句柄取消</remarks> function Insert(AIndex:Integer;AProc: TQJobProcG; AData: Pointer; AInMainThread: Boolean = False; AFreeType: TQJobDataFreeType = jdfFreeByUser): Boolean; overload; {$IFDEF UNICODE} /// <summary>插入一个作业过程,如果准备内部计数器为0,则直接执行,否则只添加到列表</summary> /// <param name="AIndex">要插入的的位置索引</param> /// <param name="AProc">要执行的作业过程</param> /// <param name="AData">附加数据指针</param> /// <param name="AInMainThread">作业是否需要在主线程中执行</param> /// <param name="AFreeType">AData指定的附加数据指针释放方式</param> /// <returns>成功,返回True,失败,返回False</returns> /// <remarks>添加到分组中的作业,要么执行完成,要么被取消,不运行通过句柄取消</remarks> function Insert(AIndex:Integer;AProc: TQJobProcA; AData: Pointer; AInMainThread: Boolean = False; AFreeType: TQJobDataFreeType = jdfFreeByUser): Boolean; overload; {$ENDIF}
下面的示例代码可能帮助您理解其用法:
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,QWorker; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } procedure DoStep1(AJob:PQJob); procedure DoStep2(AJob:PQJob); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var AGroup:TQJobGroup; begin AGroup:=TQJobGroup.Create(True); AGroup.Prepare; AGroup.Add(DoStep1,nil); AGroup.Add(DoStep2,nil); AGroup.Run(); AGroup.MsgWaitFor(); AGroup.Free; end; procedure TForm1.DoStep1(AJob: PQJob); begin if IntPtr(AJob.Data)<10 then TQJobGroup(AJob.Group).Insert(0,DoStep1,Pointer(IntPtr(AJob.Data)+1)); OutputDebugString(PChar('First:== '+IntToStr(IntPtr(AJob.Data))+' times Runed')); end; procedure TForm1.DoStep2(AJob: PQJob); begin OutputDebugString(PChar('Second:== '+IntToStr(IntPtr(AJob.Data))+' times Runed')); end; end.
我们在 DoStep1 作业中,如果检查到 AJob.Data 的值<10,则插入一个自己的一个新作业做为下一个要执行的作业,这样就形成下面的输入:
First:==0 times Runed First:==1 times Runed First:==2 times Runed First:==3 times Runed First:==4 times Runed First:==5 times Runed First:==6 times Runed First:==7 times Runed First:==8 times Runed First:==9 times Runed First:==10 times Runed Second:==0 times Runed
如果不使用 Insert,而使用Add,则会形成下面的输出:
First:==0 times Runed Second:==0 times Runed First:==1 times Runed First:==2 times Runed First:==3 times Runed First:==4 times Runed First:==5 times Runed First:==6 times Runed First:==7 times Runed First:==8 times Runed First:==9 times Runed First:==10 times Runed
【更新级别】
可选
【特别感谢】
芒果