471,603 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,603 software developers and data experts.

InvalidApartmentStateChange was detected - any ideas

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


*** Sent via Developersdex http://www.developersdex.com ***
Feb 7 '06 #1
2 2761
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?

<GG> wrote in message news:ed**************@TK2MSFTNGP15.phx.gbl...
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


*** Sent via Developersdex http://www.developersdex.com ***

Feb 8 '06 #2
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 news:ed**************@TK2MSFTNGP15.phx.gbl...
|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
|
|
|
|
|
|
|
|
| *** Sent via Developersdex http://www.developersdex.com ***
Feb 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chandika | last post: by
reply views Thread by Rob | last post: by
32 posts views Thread by Clunixchit | last post: by
1 post views Thread by Mustafa Rabie | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.