一次将作业分配给BackgroundWorker线程一个

发布于 2024-12-04 15:14:37 字数 387 浏览 0 评论 0原文

我的 silverlight 应用程序从 Web 服务(异步)获取文件集。 webservice 方法接受文件名数组并返回文件集(也作为数组)。 silverlight 客户端对文件集发出多个此类请求。

客户端同时向 Web 服务发出许多请求。我需要客户端一个 BackgroundWorker 线程来处理接收到的文件集 一个接一个

我如何在收到文件集时收集所有文件集并将这些文件集一次一个地提供给 BackgroundWorker 线程。

编辑: 我无法运行多个 BackgorundWorkers,因为文件集处理模块不是线程安全的。

My silverlight application fetches file sets from a webservice (async). The webservice method accepts an array of file names and returns the set of files (also as an array). The silverlight client makes several such requests for file sets.

The client issues many requests to the webservice at once. I need a BackgroundWorker thread at the client to process received file sets one after the other.

How can I collect all the file sets as and when they receive and give these sets to the BackgroundWorker thread one at a time.

EDIT:
I could not run multiple BackgorundWorkers as the file set processing module is not thread-safe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2024-12-11 15:14:37

使用 BlockingCollection / ConcurrentQueue 用于保存有关要处理的文件集的信息...在您刚刚拥有的后台工作程序中while 循环使下一个文件集出队并处理它...提到的集合是线程安全的并且非常快,因为大多数操作都是无锁实现的...

Use a BlockingCollection / ConcurrentQueue to hold the information about file sets to be processed... in the backgroundworker you just have while loop dequeuing the next file set and processing it... the mentioned collections are threadsafe and really fast since most operations are implemented lock-free...

坚持沉默 2024-12-11 15:14:37

backgroundworker没有内置的监听机制。它应该执行一个长动作并终止。

一种解决方案可能包括为每个文件集启动一个backgroundworker

如果这些文件集的处理必须同步,您可以决定将每个请求推送到队列中(基本上是一个数组。确保同步对其的访问)。每当您的backgroundworker处理完一个文件集时,它就会向主线程报告(ProgressChanged事件IIRC)并循环遍历数组中进一步可能的请求。每当数组为空时,工作人员就会退出。

但请注意:如果工作人员在您发送请求时退出,则会遇到问题。这就是为什么基本线程可能比后台线程更强大,特别是当您不知道是否还有更多文件集需要处理时。这一切都取决于您的工作流程。

The backgroundworker has no built-in Listen mechanism. It is supposed to perform a long action and terminate.

One solution could consist in firing up one backgroundworker for each file set.

If the processing of those file sets must be synchronized, you could decide to push each request into a queue (basically an array. Make sure you synchronize access to it). Whenever you backgroundworker is done processing a file set, it would report to the main thread (ProgressChanged event IIRC) and loop over further possible requests in the array. Whenever the array is empty, the worker exits.

Pay attention though: If the worker exits while you are sending a request, you'll have a problem. That's why a basic thread may prove stronger than a background thread, especially if you can't know whether there will be further file sets to process. It all depends on your workflow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文