Operation cannot be performed while executing asynchronously - 800a0e7f

Environment-

Windows OS [2000, XP, 2003 SP1]
MySQL - 4.0.12
ODBC - 3.51.06
MDAC 2.6/2.8
DB is on local machine

I have a small windows Win32 application which is required to insert records in MySQL DB at a very fast rate, almost 50-60 inserts [sometimes even at a higher rate] in different tables every second. I
am using MSADO here.

The connection is opened only once and is kept open till the application dies.

I have a worker thread whose responsibility is to insert records in various tables. The main thread of the application posts the insert queries to this thread using PostThreadMessage API. The worker thread uses a message loop and waits for new messages and processes them in the order in which they were posted to the thread.

The worker thread is something like this -

while(GetMessage())
{


try{
m_pCmd->ActiveConnection = m_pConnection;
m_pCmd->CommandText = strCmd;
m_pCmd->CommandType = adCmdText;
m_pCmd->Execute(NULL, NULL, adCmdText);

}

catch(_com_error &comerror)
{

}


}

Sometimes, suddenly the try block throws an exception - 800a0e7f the description says “Operation cannot be performed while executing asynchronously”. Using debug logs I have verified that there is no problem with either m_pConnection, m_pCmd or the adCmdText when exception occurs.

If I handle this exception, I can continue to insert records.

One approach that I took to debug the problem was to remove the worker thread and insert records directly from the main thread. The idea was to get away with the message loop and this seems to have worked. So, I strongly feel that the exception occurs only when I am inserting records from the message loop.

Another point that I have noticed [though, I am not very sure] is that the exception is noticed less often on machines running Windows 2003 SP1 and with MDAC version 2.82.1830

Any help on the root cause and resolution of the above exception?

Thanks much,

Sachin