关于线程/背景工问题 [英] Question regarding threading/background workers

查看:118
本文介绍了关于线程/背景工问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我身边有线程和后台工作人员的问题,我希望你能帮忙。

I have a question around threading and background workers that I hope you can help with.

我打算使FTP应用程序将文件上载到50台服务器。而不必等待每个上传完成下一个开始我一直在寻找的线程/后台工作人员,然后用户。一旦上传完成后我要举报上传的状态已完成/失败回到UI。从我的理解,我将需要使用背景工人这让我知道什么时候该任务已完成。我知道有线程我可以用生产者/消费者队列或信号同时运行的线程一定量,但我不能肯定我怎么能与回地面的工人做到这一点。

I plan on making an ftp application to upload a file to 50 servers. Rather than the user having to wait for each upload to finish before the next one starts I was looking at threading/background workers. Once an upload finishes I want to report the status of the upload "completed/failed" back to the UI. From my understanding, I will need to use background workers for this so I know when the task has completed. I know with threading I can use producer/consumer queue or a semaphore to run a given amount of threads at once but I am not quite sure how I can achieve this with back ground workers.

所以我的问题是,什么是背景的工人控制上载在一次运行的合理数量,这将是排队休息的最好方法是什么?

So my question is, what would be a sensible number of background workers controlling uploading to run at once and what would be the best way to queue the rest?

对所上载的文件的大小没有限制所以这可能是相当小的或高达几MB。

There is no limit on the size of the upload file so this could be quite small or up to a few MB.

在此先感谢。

编辑 - 我测试了一颗颗BackgroundWorker的每个正在运行的服务器同时性。其中,不仅仅是一个单一的BackgroundWorker的速度更快,但我不能说我完全满意运行50加后台工作人员在一次因为服务器数量可能在未来增加,我决定坚持只有一个,这似乎结果要够快。我将来可能会看到越来越多的工人的数量为2或3,但目前1似乎是足够的。感谢大家的帮助。

Edit - I tested out one backgroundworker for each server running simultaneousness. The results where faster than just a single backgroundworker but I can't say that i was fully comfortable with running 50 plus background workers at once and since the server count may increase in the future, I decided to stick with just the one, which seems to be fast enough. I may in future look at increasing the count of workers to 2 or 3 but currently 1 seems to be adequate. Thanks for everyones help.

感谢

推荐答案

这是比使用信号灯或生产者 - 消费者队列容易多了。

This is much easier than using a semaphore or producer-consumer queue.

把所有的任务队列(不必是线程安全的队列中,它只会从UI线程中使用)。

Put all your tasks in a queue (doesn't need to be a thread-safe queue, it will only be used from the UI thread).

循环从1到N,取出一个任务,并启动的BackgroundWorker 。 (一定要处理空队列,当有小于N个任务开始)。在 RunWorkerCompleted 事件,更新你的UI,出列了一个任务,并开始另一个的BackgroundWorker

Loop from 1 to N, taking out a task and starting a BackgroundWorker. (Be sure to handle the empty queue, when there were less than N tasks to begin with). In the RunWorkerCompleted event, update your UI, dequeue another task, and start another BackgroundWorker.

这篇关于关于线程/背景工问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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