Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
The problem is this. For each screenshot I create a new thread. I pass the name of the screenshot I want to receive to the stream. The thread is running and must exit. If I set the thread to complete automatically, the program closes after the first screenshot is taken. Now the code works, but the size of the memory occupied by the program is constantly growing. How to solve this problem?
procedure TForm1.GetImageBS(Lac,Sid,zadanie,UserID,ZOOM:string);
Var
..
begin
var FThread : TCEFBrowserThread := TCEFBrowserThread.Create(HtmLFileName, 1024, 1024);
FThread.OnError := Thread_OnError;
FThread.OnSnapshotAvailable := Thread_OnSnapshotAvailable;
FThread.OnTerminate := ThreadTerminated;
FThread.L:=La;
FThread.s:=si;
FThread.UserID:=UID;
FThread.zadanie:=zadanie;
FThread.HTML:=HtmLFileName;
if (ZO<>'') and TrySTRTOINT(z,Temp) then FThread.z:=zo else FThread.z:='';
FThread.Start;
end;
...
procedure TForm1.Thread_OnSnapshotAvailable(Sender: TObject);
var
TempBitmap : TBitmap;
JPEG: TJPEGImage;
SendRes:boolean;
FThread : TCEFBrowserThread absolute Sender;
begin
TempBitmap := nil;
memo1.Lines.Add('Запущен поток: '+FThread.L+'_'+FThread.s+'_'+FThread.UID+'_'+FThread.zadanie);
JPEG := TJPEGImage.Create;
if (FThread <> nil) and FThread.CopySnapshot(TempBitmap) then
begin
JPEG.Assign(TempBitmap);
Image1.Picture.Assign(JPEG);
if fileexists(extractfilepath(Application.ExeName)+'\BS_image\'+FThread.L+'_'+FThread.s+FThread.z+'.jpg') then
begin
if not (FThread.UID='') then
begin
....
end;
FThread.TerminateBrowserThread;
exit;
end;
JPEG.SaveToFile(extractfilepath(Application.ExeName)+'\BS_image\'+FThread.L+'_'+FThread.s+FThread.z+'.jpg');
JPEG.Free;
if not (FThread.UserID='') then
begin
.....
end;
FThread.TerminateBrowserThread;
end;
end;
Last edited by ПетрСоболев on Sun Jan 14, 2024 2:30 pm, edited 1 time in total.
I need to pass the name of the browser's output snapshot to the stream. And often a situation arises when you need several snapshots at the same time, but one thread cannot cope with this. That's why I create my own stream for everyone. How can I complete my task?
I wrote the code in the Thread_OnSnapshotAvailable procedure. And nothing works. Threads multiply and do not terminate. Maybe I'm doing something wrong?
procedure TForm1.Thread_OnSnapshotAvailable(Sender: TObject);
var
TempBitmap : TBitmap;
JPEG: TJPEGImage;
SendRes:boolean;
FThread : TCEFBrowserThread absolute Sender;
begin
TempBitmap := nil;
memo1.Lines.Add('Запущен поток: '+FThread.L+'_'+FThread.s+'_'+FThread.UID+'_'+FThread.zadanie);
JPEG := TJPEGImage.Create;
if (FThread <> nil) and FThread.CopySnapshot(TempBitmap) then
begin
JPEG.Assign(TempBitmap);
Image1.Picture.Assign(JPEG);
if fileexists(extractfilepath(Application.ExeName)+'\BS_image\'+FThread.L+'_'+FThread.s+FThread.z+'.jpg') then
begin
if not (FThread.UID='') then
begin
FTelegram.SetUserID(FThread.UID);
FTelegram.sendPhoto(extractfilepath(Application.ExeName)+'\BS_image\'+FThread.Lac+'_'+FThread.s+FThread.z+'.jpg','Задание:'+FThread.zadanie+' L='+FThread.L+' C='+FThread.s,SendRes);
end;
//FThread.TerminateBrowserThread;
if (FThread <> nil) then
begin
if FThread.TerminateBrowserThread then
FThread.WaitFor;
FreeAndNil(FThread);
end;
exit;
end;
JPEG.SaveToFile(extractfilepath(Application.ExeName)+'\BS_image\'+FThread.L+'_'+FThread.s+FThread.z+'.jpg');
JPEG.Free;
if not (FThread.UserID='') then
begin
FTelegram.SetUserID(FThread.UID);
FTelegram.sendPhoto(extractfilepath(Application.ExeName)+'\BS_image\'+FThread.L+'_'+FThread.s+FThread.z+'.jpg','Задание:'+FThread.zadanie+' L='+FThread.L+' C='+FThread.s,SendRes);
end;
//FThread.TerminateBrowserThread;
if (FThread <> nil) then
begin
if FThread.TerminateBrowserThread then
FThread.WaitFor;
FreeAndNil(FThread);
end;
end;
end;
Keep a list of TCEFBrowserThread instances in the main form and add each new instance as soon as you create it.
When a thread must be destroyed send a custom windows message to the main form with the ThreadID as one of the parameters.
Implement a procedure in the main form that is executed when the form receives the custom message, search the ThreadID value in the thread list and destroy that thread.
In this way the threads are created and destroyed in the main application thread.