Good time of the day, forum users.
I just read about the threads, so I solved the problem: there is a list of files (in stringGrid ), each file is processed (very long) by a certain algorithm.
The code is like a worker, just, maybe, something overlooked. Look, please: I decided to create 2 streams (1st for file enumeration, 2nd for processing each file).
type T_ListFiles = class(TThread) private PrProgress3: integer; procedure SetProgress3; protected procedure Execute; override; end; type T_ProcCalculate = class(TThread) private PrProgress4: integer; procedure SetProgress4; protected procedure Execute; override; end; PrSemaphore3: THandle; PrThLF: T_ListFiles; PrThPC: T_ProcCalculate; //begin T_Thread1--------------------------------------------------------------- procedure T_ListFiles.Execute; {перебор файлов, кол-во = 3шт.} var i: integer; begin for i := 1 to 3 do begin PrProgress3 := i; Synchronize(SetProgress3); if PrThLF.Terminated then begin PrThLF.Free; exit; end; PrThPC := T_ProcCalculate.Create(false); WaitForSingleObject(PrSemaphore3, infinite); end; CloseHandle(PrSemaphore3); PrThLF.Free; PrThPC.Free; end; procedure T_ListFiles.SetProgress3; begin Form1.Label6.Caption := IntToStr(PrProgress3); end; //end T_Thread1----------------------------------------------------------------- //begin T_Thread2--------------------------------------------------------------- procedure T_ProcCalculate.Execute; {обработка файла} var i: integer; begin for i := 0 to 100 do begin PrProgress4 := i; sleep(100); if PrThPC.Terminated then begin PrThPC.Free; exit; end; Synchronize(SetProgress4); end; ReleaseSemaphore(PrSemaphore3, 1, nil); end; procedure T_ProcCalculate.SetProgress4; begin Form1.Gauge2.Progress := PrProgress4; end; //end T_Thread2----------------------------------------------------------------- procedure TForm1.FormCreate(Sender: TObject); begin PrSemaphore3 := CreateSemaphore(nil, 0, 1, nil); end; procedure TForm1.StartClick(Sender: TObject); begin PrThLF := T_ListFiles.Create(false); end; procedure TForm1.abortClick(Sender: TObject); begin PrThPC.Terminate; PrThLF.Terminate; end; Thanks in advance.
Update
I agree, I have bent over multithreading (although it was probably possible to split the list of files into about 10 streams and significantly reduce the total processing time).
But without a few threads in my application, I will not get along, for the one reason that the application "hangs" during the processing of the file.
Но все равно вопрос остается: я все правильно написал? - Yes, I use threads, solely so as not to freeze the interface.
- Since I was just so close to the threads, I decided to look at the relationship between them + the semaphore work (if it doesn’t slow down the work, then I’ll leave it for now)
- I drove CloseHandle several times, until I saw it, but I still did not drive the program
- And here it is possible in more detail , about the logic of creation / decrement of the counter / release of the semaphore, what exactly is wrong?
- And another question: I understand correctly, that the flow doesn’t care which semaphore to use - it uses the first one it gets (that is, the stream does not bind to the semaphore and vice versa)?