Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi All,

I have a winform app in VS - 2008, It is client server based application using .net remoting 2.0.

On My server I have a Method which would be called from client.

Now in this method, I want to update value of Datagrid in one of my winform. Here I am getting an error:
"An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."

I tried converting my thread to STA using SetApartmentState but was not able to do so...

Please provide a solution
Regards,
Nitin Verma
Posted
Comments
Nitin_Verma 29-Jul-11 3:45am    
Thanks for the responses both of you, Actually I don't have any thing to do with Activex Control.

I am just accessing a datagridview control from a remoting class
<pre lang="vb">
Public Class ServerService
Inherits MarshalByRefObject
Implements IServerManager
Public Function MyFunction() As Integer Implements IndianetAPI.IServerManager.MyFunction
MyFunction=FrmMain.gridView(0,0).Value
End Function

End Class
</pre>
And Now in my above code snippet when I Call FrmMain.gridView above error is raised.


"An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."


Nitin Verma

.NET requires that you make your apps STA to be able to use ActiveX controls. Mark your main method with the [STAThread] attribute.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Jul-11 15:46pm    
I voted 4 because in most cases it won't be possible. The problem is: you need to have the whole application in one apartment state and ActiveX component in another one.

Therefore, more general solution is required. Please see my answer -- it works in all cases.
--SA
[no name] 27-Jul-11 15:56pm    
ok. I did not want to confuse the OP. Anyway, nice suggestion.
Sergey Alexandrovich Kryukov 27-Jul-11 16:30pm    
Well, something's wrong here. I see OP is using System.Windows.Forms. If one applies [System.MTAThread] or no attribute (which woul default to System.MTAThread) to the entry point Forms can work, but using some controls may would make it impossible (by the same reason as with that ActiveX). At the same time, creation a Forms project from template creates [System.MTAThread] application. By some reason, OP modified it.
So, we don't know how OP ran into this problem. Maybe, there was some particular reason for [System.MTAThread], or else the attribute could be accidentally removed. As the situation is not quite clear to us, I think OP should know about general resolution of the problem.

--SA
Shameel recommended you to use [STAThread]. This is not always possible, as your application might require [MTAThread]. In this case, here is what you can do: create a separate thread and set its apartment state to System.Threading.ApartmentState.STA using System.Threading.Thread.SetApartmentState. You can do it in the thread where you create a thread, not in the created thread, and you should do it before the thread is started.

In the body of the new thread, create and use your ActiveX object.

See:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.thread.setapartmentstate.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.apartmentstate.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900