When I am using AsyncCalls, how do you best get the exception into the main thread? For example:
procedure TUpdater.needsToGoFirst();
begin
asyncCall := TAsyncCalls.Invoke(procedure
begin
//some stuff
if (possiblyTrueCondition = True) then
raise Exception.Create('Error Message');
//more stuff
TAsyncCalls.VCLSync(procedure
begin
notifyUpdates();
end);
end);
end;
procedure TUpdater.needsToGoSecond();
begin
asyncCall2 := TAsyncCalls.Invoke(procedure
begin
//initial stuff
asyncCall.Sync();
//stuff that needs to go second
TAsyncCalls.VCLSync(procedure
begin
notifyUpdates();
end);
end);
end;
I know calling asycCall.Sync
will throw the exception for me, but due the the way I'm currently having my thread notify the main thread that updates have been made, I really don't have anywhere that I'm calling Sync
in the main thread. Doing so also proves difficult because I actually have another thread that is calling Sync
to make sure some things are set before acquiring resources the first thread should process first.
Do I need to wrap the innards of these functions with a try-catch and use a VCLSync
to get the exception to the main thread myself? Is is there a better way to check for exceptions in a main thread idle loop of some kind?
Edit 1
Another thought I had was to create a loops whose only job is to check the IAscynCall references for exceptions and use that to Raise the exception to the main thread, rather than duplicating that code in every post. The needsToGoSecond
metod may still get to the exception first, but it will then hold the exception and the loop would catch it there.