Page 1 of 1

Example of TMSQuery TDBGrid and thread

Posted: Mon 15 Feb 2010 15:35
by brace
May you post or describe an example on how to update a dataaware conponent (like a TDBGrid) using a TMSquery executed in a thread?

Basically a simple example in which i click a button, a query is run (for example select * from sysprocesses from sql server master db) and when the query returns the DBGrid is updated?

Thanks.

Posted: Tue 16 Feb 2010 09:13
by Dimon
When TMSQuery is executed in a separated thread you can use TDBGrid the same way as TMSQuery is executed in the main thread. TDBGrid gets data from TMSQuery in the same way in both cases.

Posted: Tue 16 Feb 2010 12:02
by brace
Is it safe to do the following?

Query is a TMSQuery placed on the form.
a DBGrid (I am using a cxGrid from DevExpress by the way) datasource a TMSquery are connected as usual.

constructor TQThread.Create(Query: TMSQuery);
begin
inherited Create(True);
FQuery := Query;
FreeOnTerminate := True;
Resume;
end;

procedure TQThread.Execute;
begin
FQuery.Open;
end;

procedure TForm5.btnThreadClick(Sender: TObject);
begin
TQThread.Create(Query1);
end;

Posted: Tue 16 Feb 2010 14:59
by Dimon
Your code is correct. SDAC can work in multithreaded applications, so it is thread safe.

Alternatively you can implement similar functionality using the NonBlocking mode or FetchAll=False mode. You can find more detailed information about this in the SDAC help.

Posted: Tue 16 Feb 2010 17:34
by brace
I tried with

TMSConnection.Options.MultipleActiveResultsSet := True

TMSQuery.Options.NonBlocking := True

but as i open the query the application "hangs", I cannot for example type in a memo. While I can do this with the above thread approach.

Am I doing something wrong?

Posted: Wed 17 Feb 2010 08:34
by Dimon
Please make sure that you use the SQL Native Client provider. For this set the TMSConnection.Options.Provider property to the prNativeClient value.
Also you can use FetchAll=False mode.

Posted: Wed 17 Feb 2010 09:44
by brace
But with FetchAll=False I don't have all the records in the grid.

I tried as you suggest, this is from dfm, anyway as I open the TMSQuery I cannot do any other thing (while I can do it when I use threads explicitly as described above).

object MSConnection1: TMSConnection
Options.MultipleActiveResultSets = True
Options.Provider = prNativeClient
Options.NativeClientVerison = ncAuto // this is not published in the object inspector
end
object MSQuery1: TMSQuery
Connection = MSConnection1
Options.NonBlocking = True
end

Posted: Wed 17 Feb 2010 10:01
by Dimon
Ok, use threads.

Posted: Wed 17 Feb 2010 13:15
by brace
So the non blocking feature is not a substitute od threads.