.Net Remoting 2.0在远程调用中访问winform控制值 [英] .Net Remoting 2.0 Accessing winform control value i a remote call
问题描述
大家好,
我在VS-2008中有一个winform应用程序,它是使用.net remoting 2.0的基于客户端服务器的应用程序.
在我的服务器上,我有一个可以从客户端调用的方法.
现在,在这种方法中,我想在我的一个winform中更新Datagrid的值.在这里我得到一个错误:
创建表单时发生错误.有关详细信息,请参见Exception.InnerException.错误是:由于当前线程不在单线程单元中,因此无法实例化ActiveX控件"8856f961-340a-11d0-a96b-00c04fd705a2". "
我尝试使用SetApartmentState将线程转换为STA,但无法执行此操作...
请提供解决方案
问候,
Nitin Verma
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
推荐答案
.NET要求您使应用程序STA能够使用ActiveX控件.用[STAThread]
属性标记您的主要方法.
.NET requires that you make your apps STA to be able to use ActiveX controls. Mark your main method with the [STAThread]
attribute.
Shameel建议您使用[STAThread]
.这并非总是可能的,因为您的应用程序可能需要[MTAThread]
.在这种情况下,您可以执行以下操作:创建一个单独的线程,并使用System.Threading.Thread.SetApartmentState
将其单元状态设置为System.Threading.ApartmentState.STA
.您可以在创建线程的线程中执行此操作,而不是在创建的线程中执行,应该在启动线程之前执行该操作.
在新线程的主体中,创建并使用您的ActiveX对象.
参见:
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
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 toSystem.Threading.ApartmentState.STA
usingSystem.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
这篇关于.Net Remoting 2.0在远程调用中访问winform控制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!