| Exibir mensagem anterior :: Exibir próxima mensagem |
| Autor |
Mensagem |
adriano_servitec Colaborador

Registrado: Sexta-Feira, 30 de Janeiro de 2004 Mensagens: 17618
|
Enviada: Ter Out 02, 2018 11:32 am Assunto: Usar thread no update |
|
|
Como criar um thread para fazer update na tabela
tentei assim
| Código: |
type
TRecDB = class(TThread)
protected
procedure Execute; override;
public
//constructor Create;
//destructor Destroy; override;
end;
TdmCadastros = class(TDataModule)
private
procedure RecDB;
********
/// Chamando a classe TRecDB que herda da Thread
procedure TdmCadastros.RecDB;
var
RecDB: TRecDB;
begin
RecDB := TRecDB.Create(True);
with RecDB Do
begin
FreeOnTerminate := True;
Priority := tpNormal;
Resume;
end;
end;
/// Como ja estava sendo executado o comando update e select aqui neste método eu apenas mudei para chamar a /// execução da thread
procedure TdmCadastros.UpdateTemSync(const tabela: String; iTemSync: Integer);
begin
lTabela := tabela;
liTemSync := iTemSync;
RecDB;
end;
/// Execute da thread que fica na classe do DataModule
procedure TRecDB.Execute;
begin
inherited;
CoInitialize(nil);
try
while not Terminated do
begin
with dmCadastros do
begin
QryPesquisa.Close;
try
QryPesquisa.SQL.Clear;
QryPesquisa.SQL.Text :=
' SELECT * FROM &TABELA WHERE temsync ISNULL ';
QryPesquisa.MacroByName('TABELA').AsRaw := lTabela;
QryPesquisa.Open();
if not QryPesquisa.IsEmpty then
begin
qryUpdate.SQL.Clear;
qryUpdate.SQL.Text := ' UPDATE &TABELA SET temsync = :Ptemsync ' +
' WHERE temsync ISNULL ';
qryUpdate.MacroByName('TABELA').AsRaw := lTabela;
qryUpdate.ParamByName('Ptemsync').AsInteger := liTemSync;
qryUpdate.ExecSQL;
qryUpdate.Close;
end;
finally
QryPesquisa.Close;
end;
end;
end;
finally
CoUninitialize;
end;
end;
|
_________________ Jogo seu smartphone? Acesse o link e confira.
https://play.google.com/store/apps/details?id=br.com.couldsys.rockdrum
https://play.google.com/store/apps/details?id=br.com.couldsys.drumsetfree |
|
| Voltar ao Topo |
|
 |
rodrigoprado Colaborador

Registrado: Terça-Feira, 24 de Mai de 2005 Mensagens: 1074 Localização: Curitiba/PR
|
Enviada: Seg Out 08, 2018 2:18 pm Assunto: |
|
|
Blz, tem um jeito que faço umas threads que achei mais simples de implementar...
https://pt.stackoverflow.com/questions/105396/como-criar-uma-anonymous-thread-em-delphi
| Código: | var
Thread1: TThread;
Thread1 := TThread.CreateAnonymousThread(
procedure
begin
btn01.Click;
end
);
Thread1.OnTerminate := proOnTerminateThread1;
mmoLog.lines.Add('Thread 1 iniciada ' + Thread1.ThreadID.ToString);
Thread1.Priority := tpHigher;
Thread1.FreeOnTerminate := True;
Thread1.Start; }
ou
TThread.CreateAnonymousThread(
procedure
begin
// seu codigo
end).start(); |
da pra separar o procedimento dentro na criação e executar, até dar as prioridades, procure por AnonymousThread, no seu caso jogaria o update dentro pro procedimento, creio que de para tentar uma adaptação sem escrever uma classe inteira de thread _________________ - Compartilhe seus conhecimentos - |
|
| Voltar ao Topo |
|
 |
johnny-walker Moderador


Registrado: Sábado, 4 de Outubro de 2003 Mensagens: 10653 Localização: Contagem/MG - BRAZIL
|
Enviada: Qua Out 10, 2018 11:44 pm Assunto: |
|
|
Só tome cuidado para utilizar uma conexão separada da principal para não ter problemas.
Veja blog de erick sasse.
http://ericksasse.com.br/
Mudou, mas o conteúdo é bom.
Lá você encontra esta informação, como tem muito tempo, não me lembro dos detalhes.
bye _________________ P.O.W.E.R B.Y D.E.L.P.H.I |
|
| Voltar ao Topo |
|
 |
|