sta

Is STA Message Loop Required in This Case?

爷,独闯天下 提交于 2019-12-17 20:56:05
问题 I've got some COM objects I'm creating and running on threads in a .NET application. The threads are marked as Single Threaded Apartments, and everything seems to be working. My understanding is that if these threads try to access COM objects from the main thread then those objects will automatically be marshaled and serialized for me in .NET, so even in that case things will be handled for me, all safe and neat, though maybe a bit slowly. My question is, while things appear to be working

How to run unit tests in STAThread mode?

佐手、 提交于 2019-12-17 17:59:40
问题 I would like to test an app that uses the Clipboard (WindowsForms) and I need the Clipboard in my unit tests also. In order to use it, it should run in STA mode, but since the NUnit TestFixture does not have a main method, I don't know where/how to annotate it. 回答1: For NUnit 2.2, 2.4 (See simple solution below for 2.5): Add an app.config file to the project containing your unit tests and include the following: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections>

Why does WPF require a STAThread attribute to be applied to the Main method?

谁都会走 提交于 2019-12-17 15:57:13
问题 I am new to WPF, and in every tutorial I read, they either have a [System.STAThread] attribute applied to their Main method, or they tell the reader to do that. Is this attribute really "required"? And if so, why? 回答1: This is more a Windows requirement than a WPF one, and goes back to the original design of Windows forms and controls, from before .NET. STAThread refers to "Single-Threaded Apartments" which refers to the threading model used by the current (main) thread. The threading model

Why are WinForms applications STAThread by default?

懵懂的女人 提交于 2019-12-17 13:54:38
问题 When you create an empty WinForms application with Visual Studio, the template has the STAThread attribute in the main application class. I have been reading some docs about it, but I'm not sure if I understood it at all. Really I have some questions about it: Why is this attribute added? What does it mean? What happens if you remove this attribute? 回答1: 1. Why is this attribute added? Because it is required by the ActiveX object model. And you can drop ActiveX controls on a WinForm (so it is

Why are WinForms applications STAThread by default?

南楼画角 提交于 2019-12-17 13:54:34
问题 When you create an empty WinForms application with Visual Studio, the template has the STAThread attribute in the main application class. I have been reading some docs about it, but I'm not sure if I understood it at all. Really I have some questions about it: Why is this attribute added? What does it mean? What happens if you remove this attribute? 回答1: 1. Why is this attribute added? Because it is required by the ActiveX object model. And you can drop ActiveX controls on a WinForm (so it is

Why are WinForms applications STAThread by default?

為{幸葍}努か 提交于 2019-12-17 13:54:12
问题 When you create an empty WinForms application with Visual Studio, the template has the STAThread attribute in the main application class. I have been reading some docs about it, but I'm not sure if I understood it at all. Really I have some questions about it: Why is this attribute added? What does it mean? What happens if you remove this attribute? 回答1: 1. Why is this attribute added? Because it is required by the ActiveX object model. And you can drop ActiveX controls on a WinForm (so it is

How do I determine the internal HWND used by COM in my current process?

故事扮演 提交于 2019-12-14 04:14:36
问题 I want to Post messages directly to the HWND that's owned by COM in my process. How do I get the HWND that COM is using in single-threaded-apartment mode? 回答1: Try this: HWND prevWindow = NULL; HWND hwnd; for ( ;; ) { hwnd = FindWindowEx( HWND_MESSAGE, prevWindow, L"OleMainThreadWndClass", NULL ); if ( !hwnd ) break; if ( GetWindowThreadProcessId( hwnd, NULL ) == GetCurrentThreadId() ) break; prevWindow = hwnd; WCHAR className[255]; *className = 0; ::GetClassName( hwnd, className, 255 ); }

Cannot use a DependencyObject that belongs to a different thread than its parent Freezable - prism

守給你的承諾、 提交于 2019-12-14 01:05:09
问题 I have came across a problem while developing a WPF application. The application is based on Prism. The application boots using the prism bootstraper and before loading any Window, the app opens a modal dialog on a different thread (STA), and then a bunch of stuff are loaded (services and etc.) The dialog is open during that time and allows to notify the user on the progress of the application start-up process (using event aggregator to pass the updates). When loading is done, bootstraper

The calling thread must be STA, because many UI components require this WPF

北城以北 提交于 2019-12-12 23:08:02
问题 I am using the MessageBox provided by WPF Toolkit. And I get the error The calling thread must be STA, because many UI components require this new Thread(new ThreadStart(delegate { MessageBox.Show("Opeartion could not be completed. Please try again.","Error",MessageBoxButton.OK,MessageBoxImage.Error); })).Start(); How can I set the ApartmentState in this case Edit: I am trying to display a modeless MessageBox using MessageBox control of WPF Toolkit. So far the code I have is as follows: void

How to tell thread-pool to run a delegate on a `STA` thread?

本秂侑毒 提交于 2019-12-12 10:37:21
问题 I need a thread-pool for working with COM objects inside an ASP.NET project. QueueUserWorkItemSTA(WaitCallback) 回答1: From the CodeProject Article of Smart Thread Pool: Also note that the .NET ThreadPool doesn't support calls to COM with single threaded apartment (STA), since the ThreadPool threads are MTA by design. So I assume if you give the Smart Thread Pool a try, it could fit your requirements. Personally, I use this class successfully since severals years. 来源: https://stackoverflow.com