sta

Starting an STAThread in C#

两盒软妹~` 提交于 2019-11-27 01:49:07
问题 I am still kind of new to C#, and especially threading in C#. I am trying to start a function that requires a single threaded apartment (STAThread) But I am not able to compile the following code: The function looks as follows in a separate class called MyClass : internal static string DoX(string n, string p) { // does some work here that requires STAThread } I have tried the attribute [STAThread] on top of the function but that does not work. So I am trying to create a new Thread as follows:

How to run something in the STA thread?

拟墨画扇 提交于 2019-11-27 00:56:19
In my WPF application I do some async communication (with server). In the callback function I end up creating InkPresenter objects from the result from server. This requires the running thread to be STA, which apparently it currently isn't. Therefore I get the following exception: Cannot create instance of 'InkPresenter' defined in assembly [..] The calling thread must be STA, because many UI components require this. Currently my async function call is like this: public void SearchForFooAsync(string searchString) { var caller = new Func<string, Foo>(_patientProxy.SearchForFoo); caller

.NET Windows Service needs to use STAThread

别来无恙 提交于 2019-11-26 16:04:54
问题 I have created a Windows Service that will be calling out to some COM components, so I tagged [STAThread] to the Main function. However, when the timer fires, it reports MTA and the COM calls fail. How can I fix this? using System; using System.Diagnostics; using System.ServiceProcess; using System.Threading; using System.Timers; namespace MyMonitorService { public class MyMonitor : ServiceBase { #region Members private System.Timers.Timer timer = new System.Timers.Timer(); #endregion #region

Single-threaded apartment - cannot instantiate ActiveX control

瘦欲@ 提交于 2019-11-26 14:32:53
I need to get information about applied CSS styles in HTML page. I used AxWebBrowser and iterate IHTMLDOMNode. I'm able to get all the data I need and move the code into my application. The problem is that this part is running inside of the background worker and I got exception when trying to instantiate the control. AxWebBrowser browser = new AxWebBrowser(); ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. Is there any way how to solve this or other option than AxWebBrowser? JaredPar The problem you

How to run something in the STA thread?

纵饮孤独 提交于 2019-11-26 09:30:01
问题 In my WPF application I do some async communication (with server). In the callback function I end up creating InkPresenter objects from the result from server. This requires the running thread to be STA, which apparently it currently isn\'t. Therefore I get the following exception: Cannot create instance of \'InkPresenter\' defined in assembly [..] The calling thread must be STA, because many UI components require this. Currently my async function call is like this: public void

STAThread and multithreading

Deadly 提交于 2019-11-26 08:55:32
问题 From the MSDN article on STAThread: Indicates that the COM threading model for an application is single-threaded apartment (STA). (For reference, that\'s the entire article.) Single-threaded apartment... OK, that went over my head. Also, I read somewhere that unless your application uses COM interop, this attribute actually does nothing at all. So what exactly does it do, and how does it affect multithreaded applications? Should multithreaded applications (which includes anything from anyone

How can I make a background worker thread set to Single Thread Apartment?

ⅰ亾dé卋堺 提交于 2019-11-26 06:47:01
问题 I am creating an automated test running application. In this part of the application, I am working on a polling server. It works by constantly polling the web server to determine when a new automated test should be run (for nightly automated runs of our GUI application). When the polling server sees a request, it downloads all the information necessary and then executes the test run in a background worker. The problem is that part of the test run has OLE, COM, and other calls (for example,

Single-threaded apartment - cannot instantiate ActiveX control

ε祈祈猫儿з 提交于 2019-11-26 03:56:10
问题 I need to get information about applied CSS styles in HTML page. I used AxWebBrowser and iterate IHTMLDOMNode. I\'m able to get all the data I need and move the code into my application. The problem is that this part is running inside of the background worker and I got exception when trying to instantiate the control. AxWebBrowser browser = new AxWebBrowser(); ActiveX control \'8856f961-340a-11d0-a96b-00c04fd705a2\' cannot be instantiated because the current thread is not in a single-threaded

How to post messages to an STA thread running a message pump?

☆樱花仙子☆ 提交于 2019-11-25 22:27:01
问题 So, following this, I decided to explicitly instantiate a COM object on a dedicated STA thread. Experiments showed that the COM object needed a message pump, which I created by calling Application.Run() : private MyComObj _myComObj; // Called from Main(): Thread myStaThread = new Thread(() => { _myComObj = new MyComObj(); _myComObj.SomethingHappenedEvent += OnSomthingHappened; Application.Run(); }); myStaThread.SetApartmentState(ApartmentState.STA); myStaThread.Start(); How do I post messages