I am calling CreateThread from a delphi application and it seems to work, however I get a system error: code 1400, invalid window handle.
the code is as follow:
procedure TForm1.SyncFile;
var
thr: THandle;
thrID: DWORD;
begin
thr := CreateThread(nil, 0, @sync, nil, 0, thrID);
if (thr = 0) then
ShowMessage('Error creating thread');
end;
procedure sync;
begin
Connect;
Application.ProcessMessages;
SyncText;
Disconnect;
ExitThread(0);
end;
I tried with TerminateThread() in form1.syncfile but nop. Also tried with CloseHandle(thr) but no solution here either. I tried then the Delphi TThread but no solution.
What am I doing wrong? Code is appreciated.
You shouldn’t perform operations which work against a TForm (or call Application.ProcessMessages) in any thread except the main, GUI thread. The windows message pump expects all windowing operations to be done on the thread that creates the window, and no other thread.
Try to push your “work” into background threads, but leave the GUI calls on the main thread.