如何判断您是否在Main UI线程上?(在CF中) [英] How can you tell if you're on the Main UI thread? (In CF)

查看:38
本文介绍了如何判断您是否在Main UI线程上?(在CF中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在不幸的是,由于WinCE Usb设备到达/删除是通过WindowsMessages自身公开的,因此我必须确保在后台线程上创建了某个(非UI)组件.我想通过异常来断言,但是缺少断言的代码.

Now unfortunately due to the fact that WinCE Usb Device Arrival / Removal exposes itself via WindowsMessages I have to ensure that a certain (non-UI) component is not created on a background thread. I would like to assert this via an exception but am lacking the code for the assertion.

此组件创建一个MessageWindow *,并使用它来接收usb到达/删除的消息.问题是,如果有人在线程退出窗口时在后台线程上创建此组件(不一定; IsBackground = true),则会被破坏.

This component creates a MessageWindow* and uses it to receive usb arrived/removed messages. The issue is if someone creates this component on a background thread (not necessarily; IsBackground = true) when the thread exits the window will be destroyed.

有什么想法吗?

*顺便说一句,我仍然不知道为什么Form不从此类继承下来

*as an aside I still don't know why Form doesn't inherit from this class

更新

我认为我的版本1不太清楚.这是v2.

I think my version 1 wasn't very clear. So this is v2.

在线程上为此创建MessageWindow或Form时,该线程退出时,Window/Form被销毁.

When you create a MessageWindow or a Form for that matter on a thread, when that thread exits the Window/Form is destroyed.

我的组件正在创建一个隐藏的"消息窗口以拦截一些重要事件,因此,我不希望它被破坏.因此,我必须以某种方式确保创建表单的代码在主UI"线程上运行.

My component is creating a "hidden" message window to intercept some important events, ergo I do not wish for it to be destroyed. Therefore I must somehow ensure that the code that creates the form is running on the "Main UI" thread.

如果可能的话,我想避免将此组件的主要"形式的引用传递给该组件,因为它(从体系结构上来说)应该距离用户界面很远.

If possible i'd like to avoid passing down a reference to the "main" form to this component as it is (architecturally speaking) supposed to be miles away from the UI.

更新

将记录问题移至单独的问题.

Moving logging question to a separate Q.

推荐答案

好,我知道您不希望组件知道"主窗口-很有道理.

Ok, I understand that you don't want for your component to "know" about the main window -- makes sense.

如何处理:如果确保始终在主线程上实例化组件,该怎么办?您的组件将在构造函数的线程上创建它的侦听器窗口.

How about this: How about if you make sure that you always instance your component on the main thread? You component will create it's listener window on the constructor's thread.

如果这样做,则只需要确保从主线程调用构造函数即可.我正在对您的代码做一些假设,但是我猜想您的体系结构中必须有一些同时了解UI和组件的类.使用回调和主窗体的InvokeRequired/Invoke方法在此处创建组件.

If you do that, then you just need to make sure that you call the constructor from the main thread. I'm making some assumptions about your code, but I'm guessing that you must have some class in your architecture that knows about both the UI and your component. Create your component there, using a callback, and the main form's InvokeRequired/Invoke methods.

这篇关于如何判断您是否在Main UI线程上?(在CF中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆