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
DBGrid, Datasource and threaded data
-
AndreyZ
Re: DBGrid, Datasource and threaded data
Hello,
You can use the following code:
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.
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;Re: DBGrid, Datasource and threaded data
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
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
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:
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;
If you want to execute a query, you should use TMyQuery instead of TMyTable.My idea is to use MyTable.SQL & use the script as a thread.
Re: DBGrid, Datasource and threaded data
I have used the source with TMyTable. It works very fine.
Thx & Regards
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;-
AndreyZ
Re: DBGrid, Datasource and threaded data
I am glad I could help. If any other questions come up, please contact us.