Jump to content
chkaufmann

Record as result for BackgroundWorker

Recommended Posts

I define a BackgroundWorker like this:


 

  FUIWorker := Parallel.BackgroundWorker;
  FUIWorker
    .Execute(
       procedure(const AWorkItem: IOmniWorkItem)
       var
         p : TPrepareAusfuehrungen;
         r : TPrepareResult;
       begin
         p := TPrepareAusfuehrungen.Create(AWorkItem.Data.ToRecord<TPrepareSettings>);
         try
           r := p.Execute(AWorkItem.CancellationToken);
           if not AWorkItem.CancellationToken.IsSignalled
             then AWorkItem.Result := TOmniValue.FromRecord<TPrepareResult>(r);
         finally
           p.Free;
         end;
       end)
    .OnRequestDone(
       procedure(const ASender: IOmniBackgroundWorker; const AWorkItem: IOmniWorkItem)
       var
         r : TPrepareResult;
       begin
         r := AWorkItem.Data.ToRecord<TPrepareResult>;

          ......
       end);

This fails in OnRequestDone with an AV. When I change it so that the result value is just an IInterface it works.

 

So my question is, how can I make it work with a "record" result? Or should I always create an interfaced object for this?

 

Christian

Share this post


Link to post

This should work just fine, I wrote a very similar code today and there were no problems:

  FWorker :=
    Parallel.BackgroundWorker.NumTasks(1)
    .Execute(
      procedure (const workItem: IOmniWorkItem)
      var
        result: TTextBoxDetectorResults;
      begin
        var data := workItem.Data.ToRecord<TTextBoxDetectorData>;
        result := FAnalyzer_Asy.Analyze(data);
        workItem.Result := TOmniValue.FromRecord<TTextBoxDetectorResults>(result);
      end)
    .OnRequestDone_Asy(
       procedure (const Sender: IOmniBackgroundWorker; const workItem: IOmniWorkItem)
       begin
         var results := workItem.Result.ToRecord<TTextBoxDetectorResults>;
         OnAnalyzed_Asy(Self, workItem.UniqueID, results);
       end)

You will have to put together a reproducible test case.

Share this post


Link to post
On 5/9/2023 at 1:32 PM, chkaufmann said:

         r := AWorkItem.Data.ToRecord<TPrepareResult>;

r := AWorkItem.Result.ToRecord<TPrepareesult>;

  • Like 1

Share this post


Link to post
39 minutes ago, Attila Kovacs said:

r := AWorkItem.Result.ToRecord<TPrepareesult>;

oops, that's embarrassing, thanks!

 

Christian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×