DBGrid, Datasource and threaded data

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Quake_MSC
Posts: 6
Joined: Wed 18 Jul 2007 09:59

DBGrid, Datasource and threaded data

Post by Quake_MSC » Mon 02 Dec 2013 12:36

Hi,

i have an application with one grid, which is connected to a mysql table. The table contains many adress data. Therefore loading the data in the grid via Tmydatasource needs long time. While loading, the application is hanging.
I cannot realize how to code it as a thread. How get the datasource the data from a thread?

Thx & regards

AndreyZ

Re: DBGrid, Datasource and threaded data

Post by AndreyZ » Tue 03 Dec 2013 10:22

Hello,

You can use the following code:

Code: Select all

procedure TMyThread.Execute;
begin
  Form1.MyQuery1.SQL.Text := 'SELECT * FROM tablename';
  Form1.MyQuery1.Open;
  Synchronize(Form1.QueryIsOpened);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  thr: TMyThread;
begin
  MyQuery1.DisableControls;
  thr := TMyThread.Create(True);
  thr.FreeOnTerminate := True;
  thr.Start;
end;

procedure TForm1.QueryIsOpened;
begin
  MyQuery1.EnableControls;
end;
Please note that you must not use this query and connection it uses while the thread is working. If you want to do that, you should provide thread-safety on your own.

Quake_MSC
Posts: 6
Joined: Wed 18 Jul 2007 09:59

Re: DBGrid, Datasource and threaded data

Post by Quake_MSC » Wed 04 Dec 2013 08:17

Thx for your reply.
2 things:

1) i am working with a TMyTable definition. So, your example will not work as shown.
2) I tried out for another situation: i get an error with the procedure mentioned in your example code TMyThread.Execute. How can i declare correkt & where in the unit?

My idea is to use MyTable.SQL & use the script as a thread. What do you think?

Regards

AndreyZ

Re: DBGrid, Datasource and threaded data

Post by AndreyZ » Wed 04 Dec 2013 11:04

1. The example I posted above works in the same way for any dataset component (TMyQuery, TMyTable, TMyStoredProc). The only thing that differs is the way to specify a statement to execute: query, table name, or stored procedure name correspondigly.
2. You should add the following code to the interface section of your unit:

Code: Select all

TMyThread = class (TThread)
protected
  procedure Execute; override;
end;
My idea is to use MyTable.SQL & use the script as a thread.
If you want to execute a query, you should use TMyQuery instead of TMyTable.

Quake_MSC
Posts: 6
Joined: Wed 18 Jul 2007 09:59

Re: DBGrid, Datasource and threaded data

Post by Quake_MSC » Thu 05 Dec 2013 14:18

I have used the source with TMyTable. It works very fine.

Code: Select all

procedure TMyThread.Execute;
const profunction = 'Thread.Execute()';
begin
Screen.Cursor:= crAppStart;
SendMessage(FormMain.Handle,WM_SETTEXT, 0, Integer(PChar('Abfrage lauft...')));
Try
	if not FormMain.MyTableVprostatmonsum.Active
    	then FormMain.MyTableVprostatmonsum.Active:= true;
except on E: Exception do
	BEGIN
    application.MessageBox(PChar('Cannot open table!' + newline
    + FormMain.MyTableVprostatmonsum.TableName + Newline
    + 'Username:' + iniUsername + Newline + 'iniPort:' + IntToStr(iniPort) + Newline
    + 'Server:' + iniServer + Newline + 'Database:' + iniDatabase+Newline+E.Message),
    Proapplication + Blank + profunction,MB_OK or MB_ICONINFORMATION);
    END;
END;
FreeOnTerminate := True;
FormMain.MyDataSourceVprostatMonsum.DataSet.FilterOptions:= [foCaseInsensitive];
FormMain.DBAdvGridVerkauf.Filter[0].Condition:= 'name like ' + thrSuchbegriff + '%';
SendMessage(FormMain.Handle,WM_SETTEXT, 0, Integer(PChar('Abfrage beendet')));
Synchronize(ActivateControls);
Screen.Cursor:= crDefault;
Terminate;
end;
Thx & Regards

AndreyZ

Re: DBGrid, Datasource and threaded data

Post by AndreyZ » Thu 05 Dec 2013 15:05

I am glad I could help. If any other questions come up, please contact us.

Post Reply