InvalidApartmentStateChange was detected - any ideas

G

GG

I have a thread pool that calls a sybase function and getting this
message

The current thread used to have an apartment state of MTA,
but the application has CoUnitialized this thread and it is now STA.
This may cause calls on RuntimeCallableWrappers representing some
COM components to fail and may also cause COM component
that are not multi-threaded to be accessed from multiple threads
at the same time which can cause corruption or data loss.


these are the parts
//add work to thread
WaitCallback callback = delegate (object state)
{ threadedPoolInsFunction ((string)state); };
ThreadPool.QueueUserWorkItem(callback, insSqlcmd);


private static void threadedPoolInsFunction(string sybaseCmd)
{
//insert into sybase and capturing threadpool exception
try
{
AseHelper.ExecuteNonQueryCmd(sybasePtsConnString,
sybaseCmd);
}
catch (Exception eT)
{
...
}
}

//utilities
public static void ExecuteNonQueryCmd(string connectionString,string
cmdText)
{
if( connectionString == null || connectionString.Length == 0 ) throw
new ArgumentNullException( "connectionString" );
if( cmdText == null || cmdText.Length == 0 ) throw new
ArgumentNullException( "bad cmdText");
using (AseConnection connection = new
AseConnection(connectionString))
{
connection.Open();
ExecuteNonQueryCmd(connection,cmdText);
connection.Close();
}
}

Throws the thread exception on connection.Close();

Any ideas?

Thanks
 
D

Dmytro Lapshyn [MVP]

Hi,

Looks like Sybase code is too smart to manage the calling thread's apartment
state (which I believe it shouldn't do). What if you try to run the same
code on a manually created thread?
 
W

Willy Denoyette [MVP]

Where do you get this message from?
If this is an exception message, could you post the call stack?
Anyway, the message should be taken seriously.

Willy.

<GG> wrote in message |I have a thread pool that calls a sybase function and getting this
| message
|
| The current thread used to have an apartment state of MTA,
| but the application has CoUnitialized this thread and it is now STA.
| This may cause calls on RuntimeCallableWrappers representing some
| COM components to fail and may also cause COM component
| that are not multi-threaded to be accessed from multiple threads
| at the same time which can cause corruption or data loss.
|
|
| these are the parts
| //add work to thread
| WaitCallback callback = delegate (object state)
| { threadedPoolInsFunction ((string)state); };
| ThreadPool.QueueUserWorkItem(callback, insSqlcmd);
|
|
| private static void threadedPoolInsFunction(string sybaseCmd)
| {
| //insert into sybase and capturing threadpool exception
| try
| {
| AseHelper.ExecuteNonQueryCmd(sybasePtsConnString,
| sybaseCmd);
| }
| catch (Exception eT)
| {
| ...
| }
| }
|
| //utilities
| public static void ExecuteNonQueryCmd(string connectionString,string
| cmdText)
| {
| if( connectionString == null || connectionString.Length == 0 ) throw
| new ArgumentNullException( "connectionString" );
| if( cmdText == null || cmdText.Length == 0 ) throw new
| ArgumentNullException( "bad cmdText");
| using (AseConnection connection = new
| AseConnection(connectionString))
| {
| connection.Open();
| ExecuteNonQueryCmd(connection,cmdText);
| connection.Close();
| }
| }
|
| Throws the thread exception on connection.Close();
|
| Any ideas?
|
| Thanks
|
|
|
|
|
|
|
|
|
 
Top