Doubt with Thread

Asked

Viewed 391 times

3

I have a form where I have a Thread to update some images, calling her using a timer interval 19000, but it’s generating me the following ERROR only when called by timer.

Project Sistemaa.exe Raised Exception class $C0000005 with message 'access Violation at 0x00604de7: read of address 0x00000000'.

procedure Tfrm01.TimerJogaAutomaticoTimer(Sender: TObject);
var
  //Cria a variavel minhaThread do tipo TMinhaThread
  minhaThread1: TGeraResultadoParaColuna01;
begin
  //Criação do objeto
   ProgressBar1.Position := 0;
   minhaThread1 := TGeraResultadoParaColuna01.Create(False);
   minhaThread1.FreeOnTerminate := true;
end;
  • Also post Thread Run to analyze...

  • @itasouza if I could help with my answer, you can accept the answer by clicking on the left side of it. If you need any more help, let us know.

1 answer

1


The basic code to create a Thread is the following, just replace the code you write in the memo by code to change image, I tried to detail all the steps as best as possible:

//Declaração da thread 
type
  TThreadSchedule = class(TThread)
  private
    procedure ChangeImage;
  protected
    procedure Execute; override;
  end;

var  //declarar como variável global
  ThreadSchedule: TThreadSchedule;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //inicia a Thread
  ThreadSchedule := TThreadSchedule.Create(false);
end;

procedure TThreadSchedule.Execute;
begin
  inherited;
  Self.Priority := tpLower;

  //enquanto não terminar a thread faz...
  while (not Self.Terminated) do
    begin
      //chama a procedure ChangeImage; 
      Self.Synchronize(ChangeImage);
      //faz uma pausa de 19 segundos
      Sleep(19000);
    end;
end;

procedure TThreadSchedule.ChangeImage;
begin
  //Escrever aqui o código para mudar a imagem
  form1.memo1.lines.add('Image Changed'); 
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //destroy a Thread para não gerar "MemoryLeaks" por exemplo
  ThreadSchedule.Terminate;
  ThreadSchedule.WaitFor;
  ThreadSchedule.Free;
end;

I hope I helped, some doubt warns.

Browser other questions tagged

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