小编Dmi*_*vch的帖子

如何在并行线程中显示进度

我有这样的代码:

procedure TSeries.Read;
var
 ReadProc: TParallelFunction2;
 Counter:  integer;
begin
 try
  ProgressForm.Show;
  Counter  := 0;
  ReadProc :=
   procedure (i1, i2, tdi: integer)
   var
    i: integer;
   begin
    for i := i1 to i2 do
     if not FDASlice[i].PctPresent then
     begin
      TInterlocked.Increment(Counter);
      ReadSlice(i, True);
     end;
   end;

  Parallel.ForI(0, High, ReadProc, pfDivide);
 finally
  ProgressForm.HideForm;
 end;
end;
Run Code Online (Sandbox Code Playgroud)

它使用STParallel库在多线程模式下处理ReadProc过程.一切正常,但我需要向用户显示进度.我尝试将'Application.ProcessMessage'+'ProgressForm.SpTBXProgressBar1.Position:= Counter'添加到ReadProc中,或者'TThread.Synchronize'没有成功.应用程序挂起,进度未显示.

我怎么做才能显示进度?是否有可能在该代码中,或者需要将所有非主线程移动到其他位置?谢谢.

delphi parallel-processing multithreading

1
推荐指数
1
解决办法
433
查看次数