如何创建多个主线程,然后从每个主线程中生成多个线程? [英] How Do I Create Multiple Master Threads And Then Spawn Multiple Threads From Each Of The Master Thread ?

查看:108
本文介绍了如何创建多个主线程,然后从每个主线程中生成多个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个表单里面有两个tabPages。在第一个TabPage中我有多个嵌套的TabPages这些TabPages在运行时动态创建在每个TabPages中,我有一个包含许多行的DataGridView(TabPage的每个datagridview中的行数不同)。 datagridview的每一行都包含几个要发送到客户机的文件,即。每个datargridview都可以有多个客户端连接。我无法理解如何识别当前选择哪个tabPage,哪个tabPage已完成将所有文件发送到客户端。

我想的是将主线程与每个嵌套的TabPage和多个相关联每个主线程内的工作线程连接到客户端机器。也正在考虑使用哈希表来存储tabPage及其相应的线程ID,但我不确定,我也不知道如何实现它。

任何人都可以给我一些指导。

迫切需要帮助!!!

Hi,

I have a Form inside which i have two tabPages.Inside the first TabPage i have multiple nested TabPages each of these TabPages are created at runtime dynamically by the user.In each of these TabPages, i have a DataGridView containing many rows(row count differs in each datagridview of the TabPage). Every row of the datagridview contains few files which are to be sent to the client machines,i.e. every datargridview can have more than one client connection. I am not able to understand how to identify which tabPage is currently selected, which tabPage has completed sending all files to the clients .
What i am thinking is to associate a Master Thread with every nested TabPage and multiple worker threads inside each Master thread to connect to the client machine.Also is was thinking to use Hash Table to store the tabPage and its corresponding Thread ID but i am not sure and also i dont know how to implement it.
Can anyone give me a few guidelines.
In urgent need of help!!!

推荐答案

那里不是主线程这样的概念。从本质上讲,所有线程在这方面都是相同的:在任何线程中,您都可以创建并启动其他线程。 (然后线程可以有不同的优先级,公寓状态,任何东西。)



执行这些操作的方法是:1)使用线程构造函数创建线程,2)使用线程池中的线程,3)使用 BackgroundWorker

http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/5cykbwz4.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/kbf0f1ct.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/4yd16hza.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.backgroundworker [ ^ ]。



创建多个线程的想法(实际上,远远超过系统上CPU核心的总数) )会非常值得怀疑。它只能降低整体吞吐量。只有当项目应实现逻辑独立行为在时间上重叠时,才能证明每个项目(无论项目是什么)具有线程的想法是合理的。如果这只是几乎几乎相同的任务,而且可以并行完成,那么实际并行执行它将是一个不好的借口。考虑按顺序执行,以降低性能开销。



然而,一个完全不同的故事是使用TPL。它基于线程,但不同的任务不必在不同的线程上。系统可以很好地或不是很好地优化性能,但这并不像使用N个线程那样疯狂。所以,这是你可以考虑的线程的替代品。请参阅:

http://en.wikipedia.org/wiki/Parallel_Extensions#Task_Parallel_Library [ ^ ],

http://msdn.microsoft.com/en-us/library /dd460717%28v=vs.110%29.aspx [ ^ ]。



-SA
There is no such concept as "master thread". Essentially, all threads are equal in this respect: in any thread, you can create and start other threads. (And then the threads can have different priorities, apartment states, anything.)

The methods of doing these are: 1) creating a thread using thread constructor, 2) using a thread from a thread pool, 3) using BackgroundWorker:
http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx[^],
http://msdn.microsoft.com/en-us/library/5cykbwz4.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[^],
http://msdn.microsoft.com/en-us/library/kbf0f1ct.aspx[^],
http://msdn.microsoft.com/en-us/library/4yd16hza.aspx[^],
http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.backgroundworker[^].

The idea to create many threads (essentially, considerably more that the total number of CPU cores on the system) would be quite questionable. It can only reduce overall throughput. This idea of having a thread per item (whatever the item is) can be justified only if the items should implement logically independent behavior overlapping in time. If this is simply few nearly identical tasks which can be done in parallel, it would be a bad excuse for actually doing it in parallel. Think about doing it in sequence, to reduce the performance overhead.

However, a completely different story is using TPL. It is based on threads, but different tasks don't have to be on different threads. The system can properly optimize the performance, well or not so well, but this is not as crazy as using N threads. So, this is the alternative to threads you can consider. Please see:
http://en.wikipedia.org/wiki/Parallel_Extensions#Task_Parallel_Library[^],
http://msdn.microsoft.com/en-us/library/dd460717%28v=vs.110%29.aspx[^].

—SA


这篇关于如何创建多个主线程,然后从每个主线程中生成多个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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