[英]Make some work on the main thread
I'm in an application that has a lot of thread. 我在一个有很多线程的应用程序中。 In one of those thread, I need to run some lines on the main thread.
在其中一个线程中,我需要在主线程上运行一些行。
This is because windows XP frees I/O resources when the creator thread ends( Overlapped I/O ). 这是因为Windows XP在创建者线程结束时会释放I / O资源(重叠I / O)。 I need to make instantiation in a Singleton creation.
我需要在Singleton创作中进行实例化。
How could I do this? 我该怎么办?
Before an off-topic answer: 在题外答案之前:
This is not GUI related, so do not propose something like myControler.BeginInvoke
since I've no GUI element 这与GUI没有关系,所以不要提出
myControler.BeginInvoke
类的myControler.BeginInvoke
因为我没有GUI元素
You can't just run some code on main thread, because it is probably busy doing something else. 您不能只在主线程上运行一些代码,因为它可能正忙于做其他事情。 You need some framework for this task, and Windows UI messaging is an example of such framework.
您需要一些框架来完成此任务,Windows UI消息传递就是此类框架的一个示例。
If your application is non-ui, then you can implement a static message queue, put your delegate there, and continuously check that queue from the main thread, pooling the delegates from it and executing them. 如果您的应用程序是非UI的,那么您可以实现一个静态消息队列,将您的委托放置在那里,并从主线程中连续检查该队列,从中汇集委托并执行它们。
However, the better approach would be to re-evaluate your applications architecture. 但是,更好的方法是重新评估您的应用程序体系结构。 Maybe you can isolate the work with the IO object to one thread.
也许您可以将IO对象的工作隔离到一个线程中。
I finally did this: 我终于做到了:
BlockingCollection
with all IPs I need to create BlockingCollection
和我需要创建的所有IP This thread will not consume many resource since he will just be blocked on the wait of the next iteration, and it stays alive for the duration of the application. 该线程不会消耗太多资源,因为他将在下一次迭代等待时被阻塞,并且在应用程序期间一直保持活动状态。
I added a condition to do this for windows XP and lower. 我为Windows XP及更低版本添加了执行此操作的条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.