Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 26-Jul-11 23:06pm
Comments
Nitin_Verma at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

.NET requires that you make your apps STA to be able to use ActiveX controls. Mark your main method with the [STAThread] attribute.
  Permalink  
Comments
SAKryukov at 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
Shameel at 27-Jul-11 15:56pm
   
ok. I did not want to confuse the OP. Anyway, nice suggestion.
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 165
2 PIEBALDconsult 150
3 BillWoodruff 148
4 DamithSL 140
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 27 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100