andrey.b 0 Posted October 6, 2023 Hello everybody, I'm a novice with the OTL. And I've written a simple app to experiment with it. But it causes the EOSError with the "System Error. Code: 1400. Invalid window handle." message in the TOmniContainerWindowsMessageObserverImpl.PostWithRetry method. Could anybody explain what is wrong with my code and (if it is not correct) provide a fix or solution? I use Delphi 10.2.3 (Tokyo) and OTL 3.07.9. Any help is appreciated. This is my code: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses OtlParallel; {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin TThread.CreateAnonymousThread( procedure begin Parallel.Async( procedure begin Sleep(100); end); end).Start; end; end. Share this post Link to post
Guest Posted October 6, 2023 I'm guessing your Form1 doesn't have a window handle yet in its OnCreate event. Try calling HandleNeeded before creating the thread, or move your code to OnShow or somewhere else where the form already has been initialized including the window handle. Share this post Link to post
andrey.b 0 Posted October 6, 2023 Thanks for the reply, Ondrej! I have just added a Button to the form and placed the code in the OnClick handler. Unfortunately, the problem still exists. The new code is: procedure TForm1.Button1Click(Sender: TObject); begin TThread.CreateAnonymousThread( procedure begin Parallel.Async( procedure begin Sleep(100); end); end).Start; end; Share this post Link to post
Primož Gabrijelčič 223 Posted November 27, 2023 For starters - why are you running Async from a background thread and not from the main thread? 1 Share this post Link to post
andrey.b 0 Posted November 27, 2023 (edited) It was just for demo or testing purposes. Now I know that it fails because the Anonymous thread is destroyed sooner than the async procedure ends. The possible workaround is to add some delay, like Sleep(1000), after calling the TThread.CreateAnonymousThread(...).Start method. Edited November 27, 2023 by andrey.b Share this post Link to post
Primož Gabrijelčič 223 Posted December 1, 2023 Yes, it fails because the owner (anonymous thread) is destroyed before the worker (Parallel.Async). You could also run into problems because the owner thread is not processing Windows messages, which is a requirement for threads that own OTL tasks. Not in this simple example, but as soon as you start sending messages towards the owner or using OnTerminate handlers you would run into trouble. Share this post Link to post