Page 1 of 1

MyDac Component and Multithreading ...

Posted: Wed 14 Mar 2007 15:54
by saidus
Hello !!!
Can I use MyDac Component (TMyQuery,TMyCommand) in a multithreading application ... :oops:
I have a 2 tables master - detail so I want to create a function that calculate sum of items in detail table after each Post then assign this sum to a field in master table ... so can i Create a function that use TMyCommand to do this (in thread).. if you can help me giving an example..
this is what i did :
Form1::detail_tableAfterPost(....)
{
HANDLE f;
DWORD d;
f = CreateThread(0,0,CalcSum,NULL,0,d);
}
DWORD WINAPI CalcSum(LPVOID)
{ MyCommand1->Execute();
}

so when i must terminate this thread ..
thank you ... :oops:

Posted: Fri 16 Mar 2007 09:58
by Antaeus
I would suggest you to use the TThread class. The solution for Delphi may be like the following (there should not be any problems to convert this code for C++Builder):

Code: Select all

TExecThread = class(TThread)
protected
  procedure Execute; override;
end;

procedure TExecThread.Execute;
begin
  if MyCommand  nil then
    MyCommand.Execute;
end;

Code: Select all

procedure TCommandFrame.btExecInThreadClick(Sender: TObject);
begin
  MyCommand.SQL := MemoSQL.Lines;
  StatusBar.Panels[0].Text := 'Executing...';
  TExecThread.Create(False);
end;

procedure TCommandFrame.MySQLAfterExecute(Sender: TObject; Result: Boolean);
var
  s: string;
begin
  if Result then
    s := 'Success' + '  (' + MyCommand.ParamByName('cnt').AsString + ' rows count)'
  else
    s := 'Execution failed';
  MemoResult.Lines.Add(s);
  StatusBar.Panels[0].Text := 'Executed';
end;

For more information see description of the TThread class in C++Builder help

Note, you should use separate connection object for the TMyCommand object used in thread.

JCL TThread Component

Posted: Mon 19 Mar 2007 14:35
by saidus
Hello !!
I Have Only One Question...
If i must use multiple component (in my case : TMyCOmmand) in separate thrids ,must i to create so mutch ansistors of VCL TThread or no
in this case the JCL TJvTHread is so simple to use ...
thanks ...

Posted: Mon 26 Mar 2007 08:58
by Antaeus
It does not matter for MyDAC which way you choose to organize multithreading in your application. You should just not use one TMyConnection object in multiple threads simultaneously.