简体   繁体   English

在主线程上做一些工作

[英]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: 我终于做到了:

  1. I added a BlockingCollection with all IPs I need to create 我添加了一个BlockingCollection和我需要创建的所有IP
  2. When I "start" my singleton, I just add my IPs in that collection 当我“开始”我的单身人士时,我只是将我的IP添加到该集合中
  3. And I start a new thread, with the Background property to True, that iterate on this collection 然后启动一个新线程,将Background属性设置为True,然后对该集合进行迭代

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.

相关问题 如果有一些前台线程在控制台上打印内容,那么在主线程完成工作后,控制台窗口是否会关闭? - Will the console window close after the main thread have done his work if there are some foreground threads that print something on the console? 挂起主线程,执行一些回调,然后恢复主线程 - Suspend Main Thread, Execute some Callbacks, and then resume Main Thread 如何在主窗体中等待工作线程? - Howto wait work thread in the main Form? 使TaskScheduler同步并在主线程中运行 - Make the TaskScheduler synchronously and run in the main thread 延迟主线程,让新线程在加载之前工作 - Delay the main thread and let new thread to work before from loaded 主线程显示对话框时允许工作线程工作 - Allow a worker thread to work while the main thread is showing a dialog 如何让线程“回报”到主线程? - How can i make a thread “report back” to main thread? 从主线程中调用一个函数并使它与主线程中的一个函数并行工作 - Calling a function from the main thread and making it work in parallel with a function in the main thread C#:如何通过从另一个线程以某种方式发出信号来强制从主线程“调用”一个方法 - C#: How to force "calling" a method from the main thread by signaling in some way from another thread 如何制作一个C#计时器来触发主线程中的事件? - How to make a C# timer that fires events in the main thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM