如何延迟主窗体的实例化,直到响应 WinForms 应用程序中的事件?
一些背景: 我在下面的代码中遇到以下异常。
ThreadStateException:ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”无法实例化,因为当前线程不在单线程单元中。
Main() 方法标有 [STAThread] 属性。 该应用程序应该在没有窗口的情况下启动,并侦听来自第三方数据上下文共享库的事件。当事件处理程序被触发时,我希望它创建并显示表单(如果还没有)。该表单仅包含一个网络浏览器控件。
所以,我的问题是如何延迟应用程序主窗体(带网络浏览器控件)的实例化,直到触发事件处理程序?
在这种情况下,我想我可以通过将表单传递给 ApplicationContext 构造函数来从头开始创建表单,然后隐藏它,但我真的很好奇为什么这不起作用。
[STAThread]
static void Main() {
ListenerAppContext context = new ListenerAppContext();
Application.Run(context);
}
...
public class ListenerAppContext : ApplicationContext {
ThirdPartyDataContextAdapter adapter;
string UrlFormat = "http://ViewDataHere/?{0}";
public ListenerAppContext() {
adapter = new ThirdPartyDataContextAdapter();
adapter.OnSomeEvent += new OnSomeEventHandler(adapter_OnSomeEvent);
}
void adapter_OnSomeEvent(string data) {
ShowData(data);
}
void ShowData(string data) {
string url = String.Format(UrlFormat, data);
if (this.MainForm == null) {
this.MainForm = new ReportViewer(url); // Exception thrown here
} else {
((IReportView)this.MainForm).Url = url;
}
}
...
}
Some background:
I get the following exception in my code below.
ThreadStateException : ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
The Main() method is marked with the [STAThread] attribute.
The app is supposed to start up without a window and listen for an event from a third party data context-sharing library. When the eventhandler is triggered, I would like it to create and show the form, if it isn't already. The form simply houses a webbrowser control.
So, my question is how do I delay instantiation of the main form (w/ webbrowser control) of an application until an event handler is triggered?
In this instance, I suppose I could just create the form from the beginning by passing it to the ApplicationContext constructor and then just hide it, but I'm really curious as to why this doesn't work.
[STAThread]
static void Main() {
ListenerAppContext context = new ListenerAppContext();
Application.Run(context);
}
...
public class ListenerAppContext : ApplicationContext {
ThirdPartyDataContextAdapter adapter;
string UrlFormat = "http://ViewDataHere/?{0}";
public ListenerAppContext() {
adapter = new ThirdPartyDataContextAdapter();
adapter.OnSomeEvent += new OnSomeEventHandler(adapter_OnSomeEvent);
}
void adapter_OnSomeEvent(string data) {
ShowData(data);
}
void ShowData(string data) {
string url = String.Format(UrlFormat, data);
if (this.MainForm == null) {
this.MainForm = new ReportViewer(url); // Exception thrown here
} else {
((IReportView)this.MainForm).Url = url;
}
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会发现立即实例化主窗体要容易得多,但隐藏它以便用户看不到它。然后,当您想查看其中的内容时,只需将其设置为可见即可。
You may find it much easier to go ahead and instantiate the main form right away, but hide it so the users don't see it. Then, you just make it visible when you want to see what's in there.