Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>COM should be initialized only once per thread, but the <code>OnExecute</code> event is triggered multiple times during a client's lifetime.</p> <p>If you are not using thread pooling with <code>TIdTCPServer</code> (by attaching a <code>TIdSchedulerOfThreadPool</code> component to the <code>TIdTCPServer::Scheduler</code> property), then you can use the <code>TIdTCPServer::OnConnect</code> and <code>TIdTCPServer::OnDisconnect</code> events to initialize/finalize your ADO objects and then use them in the <code>TIdTCPServer::OnExecute</code> event as needed, eg:</p> <pre><code>class TMyContextData { public: TADOConnection *sql_conn; TADOQuery *pos_q; TMyContextData(); ~TMyContextData(); }; TMyContextData::TMyContextData() { sql_conn = new TADOConnection(NULL); pos_q = new TADOQuery(NULL); } TMyContextData::~TMyContextData() { delete pos_q; delete sql_conn; } void __fastcall TMyForm::tcp_serverConnect(TIdContext *AContext) { ::CoInitialize(NULL); AContext-&gt;Data = new TMyContextData; } void __fastcall TMyForm::tcp_serverDisconnect(TIdContext *AContext) { delete static_cast&lt;TMyContextData*&gt;(AContext-&gt;Data); AContext-&gt;Data = NULL; ::CoUninitialize(); } void __fastcall TMyForm::tcp_serverExecute(TIdContext *AContext) { TMyContextData *pData = static_cast&lt;TMyContextData*&gt;(AContext-&gt;Data); // use pData-&gt;sql_conn and pData-&gt;pos_q as needed... } </code></pre> <p>Or, derive a new class from <code>TIdServerContext</code> instead:</p> <pre><code>class TMyContext : public TIdServerContext { public: TADOConnection *sql_conn; TADOQuery *pos_q; __fastcall TMyContext(TIdTCPConnection *AConnection, TIdYarn *AYarn, TIdContextThreadList *AList = NULL); __fastcall ~TMyContext(); }; __fastcall TMyContext::TMyContext(TIdTCPConnection *AConnection, TIdYarn *AYarn, TIdContextThreadList *AList) : TIdServerContext(AConnection, AYarn, AList) { ::CoInitialize(NULL); sql_conn = new TADOConnection(NULL); pos_q = new TADOQuery(NULL); } __fastcall TMyContext::~TMyContext() { delete pos_q; delete sql_conn; ::CoUninitialize(); } __fastcall TMyForm::TMyForm(TComponent *Owner) : TForm(Owner) { // do this before activating TIdTCPServer tcp_server-&gt;ContextClass = __classid(TMyContext); } void __fastcall TMyForm::tcp_serverExecute(TIdContext *AContext) { TMyContext *pContext = static_cast&lt;TMyContext*&gt;(AContext); // use pContext-&gt;sql_conn and pContext-&gt;pos_q as needed... } </code></pre> <p>However, if you are using thread pooling, then multiple clients can be serviced by the same physical thread, so you should move your COM initialization into the actual thread object that manages <code>TIdContext</code> objects (you should also move your ADO objects into the thread so you can reuse them for multiple clients), eg:</p> <pre><code>class TMyADOThread : public TIdThreadWithTask { protected: virtual void __fastcall AfterExecute(); virtual void __fastcall BeforeExecute(); public: TADOConnection *sql_conn; TADOQuery *pos_q; __fastcall TMyADOThread(TIdTask *ATask = NULL, const String AName = ""); }; __fastcall TMyADOThread::TMyADOThread(TIdTask *ATask, const String AName) : TIdThreadWithTask(ATask, AName) { } void __fastcall TMyADOThread::BeforeExecute() { TIdThreadWithTask::BeforeExecute(); ::CoInitialize(NULL); sql_conn = new TADOConnection(NULL); pos_q = new TADOQuery(NULL); } void __fastcall TMyADOThread::AfterExecute() { delete pos_q; delete sql_conn; ::CoUninitialize(); TIdThreadWithTask::AfterExecute(); } __fastcall TMyForm::TMyForm(TComponent *Owner) : TForm(Owner) { // do this before activating TIdTCPServer IdSchedulerOfThreadPool1-&gt;ThreadClass = __classid(TMyADOThread); } void __fastcall TMyForm::tcp_serverExecute(TIdContext *AContext) { TMyADOThread *pThread = static_cast&lt;TMyADOThread*&gt;(static_cast&lt;TIdYarnOfThread*&gt;(AContext-&gt;Yarn)-&gt;Thread); // use pThread-&gt;sql_conn and pThread-&gt;pos_q as needed... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload