简体   繁体   English

编写多线程C#应用程序

[英]Writing a Multi-threaded C# application

I need to write an application in c# that keeps track of multiple tasks, each being implemented as a class instance running on its own thread. 我需要用c#编写一个应用程序,以跟踪多个任务,每个任务都作为在其自己的线程上运行的类实例来实现。 A user interface will be used to display the status of each task instance depending on which task I select from tree view which will display the list of tasks. 用户界面将用于显示每个任务实例的状态,具体取决于我从树状视图中选择的任务,该任务将显示任务列表。

An idea I have is to create some other class, called PropertyClass which will have an instance of the TaskClass and some properties relating to this TaskClass instance. 我的想法是创建另一个名为PropertyClass的类,该类将具有TaskClass的实例以及与此TaskClass实例相关的一些属性。 Then whenever the TaskClass instance changes its state the related property in the PropertyClass instance will get updated and then the UI will be updated with these property values from the PropertyClass when the task is selected from the Tree View list. 然后,每当TaskClass实例更改其状态时,都会更新PropertyClass实例中的相关属性,然后从Tree View列表中选择任务时,将使用PropertyClass中的这些属性值来更新UI。

There will probably be hundreds of these tasks running which will be communicating with a service on a remote machine. 这些任务可能正在运行数百个,这些任务将与远程计算机上的服务进行通信。

How else can I go about coding this solution in an efficient way? 我还能如何有效地编码此解决方案?

首先从MSDN上的Task Parallel库中阅读此文档。

I have a few suggestions. 我有一些建议。

First, you need a way to make sure you don't end up with threads blocking your app from closing. 首先,您需要一种方法来确保不会导致线程阻塞您的应用程序关闭。 One sure fire way to do this is to make sure all your threads are background threads. 一种可靠的解决方法是确保所有线程均为后台线程。 That can be a little problematic if you have to make sure a thread's work is done before it is joined or aborted. 如果必须确保在加入或中止线程之前完成线程的工作,这可能会有些问题。

Second, you could look at using the ThreadPool class which should make creating and using threads more efficient. 其次,您可以看看使用ThreadPool类,它应该使创建和使用线程更加有效。 The thread pool is there to help you manage your threads. 线程池可以帮助您管理线程。

Third, you will need a method of synchronizing your data access from the GUI to data in the other threads. 第三,您将需要一种将来自GUI的数据访问同步到其他线程中的数据的方法。 In WPF you use the Dispatcher and in WinForms you'll use Invoke. 在WPF中,您可以使用Dispatcher;在WinForms中,您可以使用Invoke。

Forth, the BackgroundWorker class can help with all of these if it'll fit in the model of your application. 第四,如果BackgroundWorker类适合您的应用程序模型,则它可以提供所有帮助。

Fifth, events and delegates can be BeginInvoked which essentially puts them on another thread. 第五,事件和委托可以被BeginInvoked所使用,这实际上将它们置于另一个线程上。 It's kind of implicit multi-threading and can be useful. 它是一种隐式多线程,非常有用。

Sixth, and I've not yet had the chance to use this, .Net 4 has the Parallel Task Library that may be of use to you. 第六,我还没有机会使用它,.Net 4具有并行任务库,可能对您有用。

Seventh, safe shared data access and synchronization can be accomplished using lock and/or Monitor. 第七,可以使用锁定和/或监控器来完成安全共享数据的访问和同步。

Hope this helps. 希望这可以帮助。

-Nate -Nate

If each TaskClass instance corresponds to a node on the tree view, you can store the TaskClass instance in the tree view item's Tag property. 如果每个TaskClass实例与树视图上的一个节点相对应,则可以将TaskClass实例存储在树视图项的Tag属性中。 Or you could create a dictionary of TaskClasses, keyed by a unique identifier such as a GUID, and store the identifier in the Tag property. 或者,您可以创建TaskClasses的词典,并用诸如GUID之类的唯一标识符作为关键字,然后将该标识符存储在Tag属性中。

In either case, use a callback method to signal that a TaskClass instance has an update. 在这两种情况下,都应使用回调方法来表示TaskClass实例具有更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM