如何从C#中的线程返回对象? [英] How to return an object from a thread in C#?

查看:85
本文介绍了如何从C#中的线程返回对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要并行处理一部分返回字符串列表(文件名)的应用程序(在C#2010中).有人可以给我指出一个很好的例子,说明如何将其放入需要输入(我已经阅读了如何做)和输出的工作线程中吗?

我已经在MFC中完成了多线程,而在.NET中完成了简单的多线程,所以我有一些经验.

#############################################

我会尽力改善这个问题.我从用户那里获得目录的路径,我想解析所有子目录并返回子目录中的PDF文件列表.这可能需要一段时间,所以我想

1>启动一个线程,为其提供顶层目录路径
2.使线程遍历所有子目录,构建n个PDF文件路径的数组
3)完成线程后,获取PDF文件数组以进行进一步处理
4)我想在线程中执行此操作的原因是,在进行目录搜索时,UI可能用于其他用途

希望这更清楚.

我想我应该只创建我的数组,对其进行锁定,然后在搜索完成后再进行访问.这样,线程不会真正返回"任何东西...

谢谢,
Tom

I need to parallelize a portion of an application (in C# 2010) that returns a list of strings (file names). Can someone point me to a good example of how to put this in a worker thread that requires both input (I''ve read about how to do that) AND output?

I''ve done multithreading in MFC and simple ones in .NET, so I have a bit of experience.

#################################################

I''ll try to improve the question. I obtain from the user a path to a directory, and I want to parse all the subdirectories and return a list of PDF files in the subdirectories. This can take quite a while so I want to

1> start a thread , giving it the top directory path
2. have the thread go through all sub-directories , building a n array of paths to PDF files
3) when the thread is done, obtain the array of PDF files for further processing
4) the reason I want to do this in a thread is that the UI may be used for some other things while the directory search is ongoing

Hope this is clearer.

I am thinking I should just create my array, put a lock on it, and then access is later when the search is done. This way the thread does not really "return" anything ...

Thanks,
Tom

推荐答案

问题没有明确定义.传统意义上的返回对象"和输出"的概念几乎没有共通之处.它的输出意味着仅获取工作线程生成的一些数据,问题是在什么时候",即同步.

让我们从一个简单的模型开始:一个工作线程将一些数据放入共享内存中,另一个线程读取最新值.有时是必需的.简单的答案是在同一对象上使用lock语句.很多时候,大多数线程主要读取,有些线程则较少写入.在这种情况下,一种特殊的互斥或锁定有助于提高性能:System.Threading.ReaderWriterLockSlim.您将获得此锁,以便先读取,如果需要写入,请升级"该锁以进行写入.
我的一篇文章说明了该类的巨大好处:希望您在这里……只有一次 [ ^ ].

如果您认为自己知道线程输入的好方法,则可能需要重新考虑.请查看线程包装的代码:
如何将ref参数传递给线程 [ ^ ].

现在,让我们回到线程输出".一种真正健壮的方法是以生产者-消费者方式进行线程通信.这可以使用System.Threading.EventWaitHandle来实现.请参阅我的通用阻止队列,该队列可用于此目的.它甚至可以用于线程间方法调用.请参阅有关以下主题的提示/技巧"文章:
用于线程通信和线程间调用的简单阻塞队列 [ ^ ].

您将找到完整的源代码和详细的用法示例.

祝你好运,
—SA
The problem is not well-defined. The notion of "return an object" and "output" in its traditional sense has little in common. It output means simply getting some data produced by a worker thread, the question is "at what moment of time", that is, a synchronization.

Let''s start from a simple model: a worker thread puts some data in a shared memory and another thread reads the latest value. Sometimes it is needed. The simple answer is using lock statement on the same object. Very often several threads mostly read, some threads write, less often. In this case a special kind of mutex or lock helps to improve performance: System.Threading.ReaderWriterLockSlim. You get this lock for read first, and if write is required, "upgrade" it for writing.
One of my articles illustrates the great benefit of this class: Wish You Were Here… Only Once[^] with code samples.

If you think you know a good way of thread input, perhaps you need to think again. Please look at the code of thread wrapper:
How to pass ref parameter to the thread[^].

Now, let''s get back to thread "output". A really robust way is thread communication in producer-consumer manner. This can be achieved using System.Threading.EventWaitHandle. Please see my generic blocking queue which can be used for this purpose. It can even be used for inter-thread method invocation. See my Tips/Tricks article on the topic:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

You will find completed source code and detailed usage samples.

Good luck,
—SA


您可以尝试使用Dispatcher 类.


这篇关于如何从C#中的线程返回对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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