コンソールアプリで、WinFormのコントロールを使用すると以下の例外が発生します。
System.Threading.ThreadStateException はハンドルされませんでした。 Message=現在のスレッドはシングル スレッド アパートメントでないため、ActiveX コントロール 'xxxx' をインスタンス化できません。 Source=System.Windows.Forms StackTrace: 場所 Program.Main(String[] args) 場所 Program.cs:行 33 場所 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 場所 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 場所 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 場所 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 場所 System.Threading.ThreadHelper.ThreadStart() InnerException: |
この場合の対処法は、Mainメソッドの上に,[STAThread]を追加しシングルスレッドアパートメントである事を明示させます。
static void Main( string[] args ) { ... } |
↓
[STAThread] static void Main( string[] args ) { ... } |
関連記事
コメントを残す