sta

Why are WinForms applications STAThread by default?

戏子无情 提交于 2019-11-28 09:43: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? TomTom 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 there for compatibility) OR some .NET classes use native controls which require that attribute. 2.

Starting an STAThread in C#

送分小仙女□ 提交于 2019-11-28 07:32:01
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: Thread t = new Thread(new ThreadStart(MyClass.DoX)); but this will not compile (The best overloaded

How to run unit tests in STAThread mode?

微笑、不失礼 提交于 2019-11-28 06:09:32
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. Bernhard Hofmann 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> <sectionGroup name="NUnit"> <section name="TestRunner" type="System.Configuration

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

China☆狼群 提交于 2019-11-27 20:42:12
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? 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 in use dictates how other .NET and COM applications will talk to your application (and inherently, its

Convert Keith Hill's PowerShell Get-Clipboard and Set-Clipboard to a PSM1 script

安稳与你 提交于 2019-11-27 17:52:31
问题 I'd like to convert Keith Hill's C# implementation of Get-Clipboard and Set-Clipboard into pure PowerShell as a .PSM1 file. Is there a way to spin up an STA thread in PowerShell as he does in his Cmdlet when working with the clipboard? The Blog Post The Code 回答1: TextBox doesn't require -STA switch. function Get-ClipBoard { Add-Type -AssemblyName System.Windows.Forms $tb = New-Object System.Windows.Forms.TextBox $tb.Multiline = $true $tb.Paste() $tb.Text } function Set-ClipBoard() { Param(

Why are WinForms applications STAThread by default?

爱⌒轻易说出口 提交于 2019-11-27 15:33:42
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? TomTom 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 there for compatibility) OR some .NET classes use native controls which require that attribute. 2.

An MTA Console application calling an STA COM object from multiple threads

纵饮孤独 提交于 2019-11-27 13:11:46
问题 Although there are many questions about COM and STA/MTA (e.g. here), most of them talk about applications which have a UI. I, however, have the following setup: A console application, which is by default Multi-Threaded Apartment (Main() explicitly has the [MTAThread] attribute). The main thread spawns some worker threads. The main thread instantiates a single-threaded COM object. The main thread calls Console.ReadLine() until the user hits 'q', after which the application terminates. A few

.NET Windows Service needs to use STAThread

和自甴很熟 提交于 2019-11-27 12:29:06
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 Construction public MyMonitor () { this.timer.Interval = 10000; // set for 10 seconds this.timer

Expression or reg-ex for java script or adobe livecycle tools

帅比萌擦擦* 提交于 2019-11-27 08:36:40
问题 What will be the expression for restricting a user to enter either 5 lines or 375 characters in a scrollable text field? I tried theese: ([A-Z])\w+ $0</a> var re,regs,val; if(OTHER.rawValue!=null) { val=OTHER.rawValue; //re=[a-zA-Z\d\s\-\,\#\.\+]+ //re=/abc(?!$){5}/ re=/\be(\w*)s\b/m{2,10}$; regs=val.match(re); if(!regs) { fieldname.rawValue=""; xfa.host.messageBox("ANY THING"); xfa.host.setFocus(fieldname) } } re=/abc(?!$){5}/ and many more... but I am not getting exact one to validate

Why are WinForms applications STAThread by default?

不想你离开。 提交于 2019-11-27 03:10:43
问题 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