哪些是在单线程单元[STA]中运行C#应用程序的方法? [英] Which are ways to run C# Application in Single Thread Apartment [STA]?

查看:113
本文介绍了哪些是在单线程单元[STA]中运行C#应用程序的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个基于C#Window Services的应用程序,其中使用需要在单线程单元[STA]上运行的ActiveX。那个ActiveX做TCP通信。



如果我将它作为MTA运行,那么它会给出异常ActiveX控件无法实例化,因为当前线程不在单线程单元中。



现在我找到了两种在STA中运行它的方法。



1)在入门级应用方法中写入[STAThread]。如下所示:

Hello,

I have one C# Window Services based application in which using ActiveX which required to be run on Single thread apartment [STA]. That ActiveX doing TCP communication.

If I run it as MTA then it gives exception "ActiveX control cannot be instantiated because the current thread is not in a single-threaded apartment."

Now I found two ways to run it in STA.

1) Write [STAThread] in entry level method of application. Like below:

[STAThread]
static void Main()







2)启动新线程并将公寓状态设置为STA如下所示






2) Start New thread and set apartment state to STA like below

Thread t = new Thread(() => StartTCPCommunication());
t.SetApartmentState(ApartmentState.STA);
t.Start();





现在我的问题是上述两种方式之间有什么区别。



因为我的应用程序结构就像我不能使用Way#1因为我的应用程序作为Windows服务运行。所以,即使我使用方式#1,它也会给我上面提到的ActiveX异常。



如果我将Windows服务转换为Windows桌面应用程序,那么相同的代码可以正常工作#1



但我需要Windows服务,所以我需要使用Way#2,但是当我使用它时,我无法进行TCP通信,甚至没有任何例外。



那么任何人都可以帮我解决问题吗?或其他方式?



提前谢谢



Now my question is what is the difference between above 2 ways.

Because my application structure is like that I can't use Way#1 becuase my appication running as Windows Service. so even if I use way#1 it give me ActiveX exception mentioned above.

If I convert windows services to Windows Desktop application then same code working fine with Way#1

But I need windows service so I need to use Way#2, but when I used that I can not do TCP communication and even not get any exception.

So can any one help me to figure out the issue ? or some alternate way ?

Thanks in advance

推荐答案

首先,这是最简单的建议:不要使用任何愚蠢和过时的ActiveX控件进行TCP通信。听听它听起来如何:ActiveX?在.NET中?说真的吗?



相反,您可以使用套接字,或者更好更简单的类 System.Net.Sockets.TcpListener 。这也是套接字API的一部分,为方便起见包装:

https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener%28v=vs.110%29.aspx

https: //msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener%28v=vs.110%29.aspx



另外,我不知道为什么你认为Way#1不能用于服务。服务应用程序也有入口点方法,因此您可以将属性 [System.STAThread] 应用于它。



当然,你总是可以使用Way#2。您没有提供任何有关它不起作用的信息,但您可能因不同原因而面临不同的问题。例如,你可以创建这个 STAThread ,但是仍然会在这个线程中做一些有问题的事情,但是在其他一些线程中。 :-)



-SA
First of all, here is the simplest advice: don't use any stupid and obsolete ActiveX controls for TCP communications. Just listen how it sounds: ActiveX? In .NET? Seriously?

Instead, you can use the sockets, or, even better and simpler, the class System.Net.Sockets.TcpListener. which is also the part of sockets API, wrapped for your convenience:
https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener%28v=vs.110%29.aspx,
https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener%28v=vs.110%29.aspx.

Also, I don't know why you decided that Way#1 cannot be used for a service. A service application also has the entry point method, so you can apply the attribute [System.STAThread] to it.

And of course you always could use Way#2. You did not provide any information on why it did not work, but you could face different problems by different reasons. For example, you could create this STAThread but still do something problematic not in this thread, but in some other thread. :-)

—SA


这篇关于哪些是在单线程单元[STA]中运行C#应用程序的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆