C# UserControl BeginInvoke 问题

发布于 2024-08-18 07:40:09 字数 268 浏览 2 评论 0原文

我有一个 C# 用户控件,它有自己的后台工作线程。该工作线程在控件的构造函数中启动,并在控件被释放时停止。

线程定期使用委托调用 BeginInvoke 方法,但有时会出现异常“在创建窗口句柄之前无法在控件上调用 Invoke 或 BeginInvoke。”

现在我问你,如何检查我的工作线程是否可以调用BeginInvoke,只要控件没有完全创建就不会调用?

仅在编译版本时才会出现此问题。不在调试模式下。

最诚挚的问候

I have got a C# user control, which has got it's own background worker thread. This worker thread is started in the constructor of the control and stopped when the control is disposed.

The thread periodically calls the BeginInvoke-Method with a delegate, but sometimes the exception "Invoke or BeginInvoke cannot be called on a control until the window handle has been created." occoures.

Now I ask you, how can I check whether calling BeginInvoke is possible from my worker thread to do no invoking as long as the control isn't completely created?

This problem only occoures when compiling a release. Not in debug mode.

with best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫雨熙 2024-08-25 07:40:09

工作线程应该在内部创建...(您有两个选择):

  1. Control.HandleCreated 事件的处理程序
  2. 覆盖 Control.OnHandleCreated

您需要知道的是,即使在之后,控件也可能尚未完全创建(准备使用)构造函数执行完毕。

The worker thread should be created inside... (you have two options):

  1. A handler of Control.HandleCreated event
  2. Overriding Control.OnHandleCreated

What you need to know is that a control may not be fully created (ready to be used) even after the constructor is done executing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文