Ok. Thanks for answer, I did it.
But, performance is exactly the same as without threading (I measured) which proves my assumption. Synchronize executes the passed method in the main thread, therefore the whole thing bacomes worthless in this case...
Does anyone has an idea how to solve this? Or maybe multithreading is not solution for my problem?
//Interface
type
TThreadRefreshDatasets=class(TThread)
private
FDataset:TZQuery;
FBookmark:TBookmark;
procedure MyRefresh;
procedure MyEnableControls;
procedure MyDisableControls;
procedure MyGetBookmark;
procedure MyGotoBookmark;
procedure MyFreeBookmark;
protected
procedure Execute; override;
public
constructor Create(pDataset:TZQuery);
end;
var
vThreadCount:Integer;
//Implementation
constructor TThreadRefreshDatasets.Create(pDataset:TZQuery);
begin
inherited Create(True);
FDataset:=pDataset;
FreeOnTerminate:=True;
Resume;
end;
procedure TThreadRefreshDatasets.MyDisableControls;
begin
FDataSet.DisableControls;
end;
procedure TThreadRefreshDatasets.MyEnableControls;
begin
FDataSet.EnableControls;
end;
procedure TThreadRefreshDatasets.MyRefresh;
begin
FDataSet.Refresh;
end;
procedure TThreadRefreshDatasets.MyGetBookmark;
begin
FDataSet.GetBookmark;
end;
procedure TThreadRefreshDatasets.MyGotoBookmark;
begin
FDataset.GotoBookmark(FBookmark);
end;
procedure TThreadRefreshDatasets.MyFreeBookmark;
begin
FDataset.FreeBookmark(FBookmark);
end;
procedure TThreadRefreshDatasets.Execute;
begin
try
if FDataset.Active=True then begin
//Get bookmark.
FBookmark:=FDataset.GetBookmark;
//Disable controls.
Synchronize(@MyDisableControls); //FDataset.DisableControls;
//Refresh the dataset.
Synchronize(@MyRefresh); //FDataset.Refresh;
//Goto bookmark
Synchronize(@MyGotoBookmark); //FDataset.GotoBookmark(FBookmark);
end;
finally
//Free bookmark.
Synchronize(@MyFreeBookmark); //FDataset.FreeBookmark(FBookmark);
//Enable controls.
Synchronize(@MyEnableControls); //FDataset.EnableControls;
vThreadCount:=vThreadCount+1;
end;
end;
procedure RefreshOrdersRequirementsDatasets;
var
I:Integer;
vElapsedTime:TDateTime;
begin
try
ShowMessage('Program will now refresh all datasets on the form. Please, be patient.');
vElapsedTime:=Now;
with FormMain do begin
//Create threads for refreshing the dataset.
TThreadRefreshDatasets.Create(ZQueryQryOrdrs);
TThreadRefreshDatasets.Create(ZQueryQryRqrmts);
TThreadRefreshDatasets.Create(ZQueryQryFrmMtrls);
TThreadRefreshDatasets.Create(ZQueryQryFrmMtrlsOrdrs);
TThreadRefreshDatasets.Create(ZQueryQryFrmOrdrs);
TThreadRefreshDatasets.Create(ZQueryQryFrmOrdrsMtrls);
end;
//Wait until all threads finish.
vThreadCount:=0;
while (vThreadCount<6) do begin
Application.ProcessMessages;
end;
vElapsedTime:=Now-vElapsedTime;
ShowMessage('Datasets refreshed. Elapsed time: '+FormatDateTime('hh:nn:ss',vElapsedTime));
except
ShowMessage('Error while refreshing datasets.');
end;
end;