开发者

Why might I be unable to create an instance of System.Windows.Forms.Form in C#?

开发者 https://www.devze.com 2023-01-09 14:23 出处:网络
I have an intermittent prob开发者_如何转开发lem where I cannot create an instance of the System.Windows.Forms.Form class in my C# library. I log exceptions in my program, but unfortunately nothing was

I have an intermittent prob开发者_如何转开发lem where I cannot create an instance of the System.Windows.Forms.Form class in my C# library. I log exceptions in my program, but unfortunately nothing was recorded for this problem. It might not be relevant, but my library, called via COM (i.e. there is no Main method).

So I threw a test exception, and it was caught and logged successfully -- so I'm thinking that maybe something screwy is going on in the message loop?

Update 1

The Form is created using the following code in a Thread which has ApartmentState.STA set.

  someForm = new Form();
  someForm.Visible = false;
  someForm.Text = "Hello world!";


It's just a guess, since you've provided no code and little context, but might you be missing the STAThread attribute on your entry point method (Main)? This is necessary for WinForms apps that make use of COM.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号