chkaufmann 17 Posted May 9, 2023 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
Primož Gabrijelčič 223 Posted May 10, 2023 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
Attila Kovacs 629 Posted May 10, 2023 On 5/9/2023 at 1:32 PM, chkaufmann said: r := AWorkItem.Data.ToRecord<TPrepareResult>; r := AWorkItem.Result.ToRecord<TPrepareesult>; 1 Share this post Link to post
chkaufmann 17 Posted May 10, 2023 39 minutes ago, Attila Kovacs said: r := AWorkItem.Result.ToRecord<TPrepareesult>; oops, that's embarrassing, thanks! Christian Share this post Link to post