Run routines in background

Asked

Viewed 1,133 times

0

I have a routine to run when opening my system once a day, I put a panel with a message for the client to see that it is being processed, but is now taking a long time to run.

My idea is to put in background, I did tests with Thread but it locks my system.

What is the best way to do this without the need to crash the user?

  • when you use a thread your system hangs? maybe you are using wrong, can better describe what that operation would be?

  • 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.

  • 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.

  • 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?

  • have this one: link, it is good that before applying to your problem you test in a simple application to see how it works.

  • 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.

Show 1 more comment

1 answer

1


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).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.