Advise on Thread code please
- From: "Scott Martin" <smartin@xxxxxxx>
- Date: Wed, 28 Feb 2007 15:15:36 -0600
I have created some thread code to display a progress using
http://www.tmssoftware.com/acp.htm and it works as expected.
In reading many articles, I would like to get some advise on what
changes would be needed to make this more solid thread code.
I have read that it is better to use messages than syncronize?
How would I implement this if so?
Are there any other pointers that would make this better?
I believe this to be textbook version of how to invoke, and I
was looking better approach from more experienced developers
using TThread.
I have seen several articles referencing thread information via
links, but many of the links are outdated/not working.
(Threadmare, etc).
The goal here is to display activity during an SQL command.
Regards,
Scott
type
TDBThread = class(TThread)
protected
aSession : TnxSession;
aDatabase: TnxDatabase;
aQuery : TnxQuery;
procedure Execute; override;
procedure LoadMyData(sTable,sWhere,sValue: String);
procedure ShowProgress;
procedure HideProgress;
private
public
constructor Create;
end;
TForm1 = class(TForm)
nxSession: TnxSession;
nxDatabase: TnxDatabase;
nxRemoteServerEngine: TnxRemoteServerEngine;
nxWinsockTransport: TnxWinsockTransport;
nxQuery: TnxQuery;
DataSource: TDataSource;
btnSELECT: TButton;
DBGrid: TDBGrid;
procedure btnSELECTClick(Sender: TObject);
private
function ConnectToServer: Boolean;
public
end;
var
Form1: TForm1;
//
DBThread: TDBThread;
implementation
uses pWaitWin;
{$R *.dfm}
procedure UpdateWaitWindow(vShow: Boolean; vMsg: String);
begin
if vShow then begin
Screen.Cursor := crHourGlass;
if fWaitWin = nil then
fWaitWin := TfWaitWin.Create(nil); // simple wait window with circular
progressbar
fWaitWin.MessagePanel.Caption := vMsg;
fWaitWin.Show;
fWaitWin.Update;
end
else begin
if fWaitWin <> nil then
fWaitWin.Close;
Screen.Cursor := crDefault;
end;
end;
constructor TDBThread.Create;
begin
inherited Create(False);
FreeOnTerminate := True;
// grab info from form vcl, already on form
aSession := Form1.nxSession;
aDatabase := Form1.nxDatabase;
aQuery := Form1.nxQuery;
end;
function TForm1.ConnectToServer: Boolean;
begin
Result := False;
try
// code to connect to the server
Result := True;
except
end;
end;
procedure TForm1.btnSELECTClick(Sender: TObject);
begin
if ConnectToServer then begin
DBThread := TDBThread.Create;
DBThread.Resume;
end;
end;
procedure TDBThread.Execute;
begin
Synchronize(ShowProgress);
LoadMyData(Form1.eTableName.Text,Form1.eWhereField.Text,Form1.eValue.Text);
Synchronize(HideProgress);
Terminate;
end;
procedure TDBThread.LoadMyData(sTable,sWhere,sValue: String);
begin
if Terminated then Exit;
with aQuery do begin
if Active then Close;
//
SQL.Clear;
SQL.Text := 'SELECT * FROM '+sTable;
if Form1.eWhereField.Text <> '' then begin
SQL.Add('WHERE '+sWhere);
SQL.Add('LIKE '+QuotedStr('%'+sValue+'%'));
end;
Open;
end;
end;
procedure TDBThread.ShowProgress;
begin
UpdateWaitWindow(True,'Loading Items, please wait ...');
end;
procedure TDBThread.HideProgress;
begin
UpdateWaitWindow(False,'');
end;
.
- Follow-Ups:
- Re: Advise on Thread code please
- From: Remy Lebeau \(TeamB\)
- Re: Advise on Thread code please
- Prev by Date: Re: The delphi magazine (HELP)
- Next by Date: Re: Indy Server -> client
- Previous by thread: Indy Server -> client
- Next by thread: Re: Advise on Thread code please
- Index(es):