ClientSocket: TCustomWinSocket; AQuery: TComponent;
Asta servers are non-blocking and event driven so normal messaging calls are not threaded. You can have ASTA launch a thread in response to a message from any ASTA client and ASTA will also make available some database components for you to use in the thread. If you do not need access to any database calls use ThreadedUtilityEvent. Set the QueryToUse parameter to the type of query component you need like ttSelect or ttExec.
The following shows the use of the ThreadedDBUtilityEvent event from an ASTA server.
Notice the AstaServerSocketSendCodedParamListSocket
call which uses the ThreadedClient copy of the UserRecord.
Note: If a client is disconnected at this point the thread will be marked as terminated, so perhaps a
Exit is needed before the actual send
This example assumes that the TestUtilSQLEvent is being called from within OnCodedParamList on the server which gets called with a ClientSocket:TCustomWinSocket and an incoming Params:TAstaParamList. In this example a request is made for a component that can handle SQLSelects by passing in ttSelect. ThreadedDBSupplyQuery is called with ttSelect to return the Query:TComponent that can be used safely in the thread. The TestUtilSQLEvent must be of type TAstaDBUtilityEvent.
The call is initiated by calling ThreadedDBUtilityEvent(ClientSocket,ttSelect,TestUtilSQLEvent,Params);
procedure TForm1.TestUtilSQLEvent(Sender: TObject;
ServerSocket: TAstaServerSocket; ClientSocket: TCustomWinSocket;
Query: TComponent; Params: TAstaParamList);
//the Sender is now a TAstaThread
TQuery(Query).SQL.Text := Params.AsString;
p := TAstaParamList.Create;
p.FastAdd(IntToStr(TQuery(Query).RecordCount) + ' records retrieved.');
p.FastAdd(CloneDataSetToString(Query as TQuery));
//don't call the AstaServerSocket1 as we are in a thread
//use the passed in ServerSocket
ThreadedClient, 1550, P);
//Asta disposes of the query
//if you want additional components you can call
//ThreadedDBSupplyQuery since you now have the Sender TAstaThread
//and the client socket
Threading Good Habits
See the AstaBDEServer for an example of how to use this method.