Use parallel processing Ttaks.
procedure executarrotina;
begin
vg_Tarefas.Add(TTask.Create(procedure ()
var vEmail : TEmail;
begin
vEmail := TEmail.Create;
try
vEmail.Assunto := pAssunto;
vEmail.Destinatarios := pDest;
vEmail.Mensagem := pMens;
vEmail.Enviar;
finally
FreeAndNil(pMens);
FreeAndNil(vEmail);
end;
end));
vg_Tarefas[vg_Tarefas.Count - 1].Start;
end;
declare the variable
var vg_Tarefas : TList<ITask>;
uses System.Generics.Collections;
Very important: Never use commands that show warnings, update Edit, in this routine. If you want performance, do not update screen. If you need to update screen, update something in a table and fire event in the bank, which will be more efficient.
In this example, I am sending email in background (parallel).
when you use a thread your system hangs? maybe you are using wrong, can better describe what that operation would be?
– ProgramandoMil
My routine runs a stored Procedure in the database to calculate document interest. Today I put a panel and lock the system for the user until it finishes, but in some customers it takes a certain time. So I want to put in a thread or something like that, the tips I found on how to use thread does not work well in my case, the system hangs and only comes back when the procedure is finished.
– Thiago Porto
It seems that the solution really would be to use a thread, another alternative would be your program to use an external program to do the operation and wait for the result of it in a text file for example, it would be like a thread, your.
– ProgramandoMil
The way I did it was based on the net tutorial, creating a Thread class and placing the procedure inside the execute method. You would have a tutorial on threads?
– Thiago Porto
have this one: link, it is good that before applying to your problem you test in a simple application to see how it works.
– ProgramandoMil
If the thread freezes the system, you may have triggered it with the execute method. It has to be with start. Another detail is that the thread cannot use the same connection as the rest of the program with the database, it has to be a connection only for it.
– Ricardo Alves Carvalho