多线程 WMI 调用 - 如何最好地处理这个问题?

发布于 2024-11-27 15:47:33 字数 577 浏览 3 评论 0原文

您好,我需要向域中的每个系统(可能是数千个)发送 WMI 查询,并且 WMI 查询似乎需要很长时间才能返回。因此,我正在审查使用多个线程发送多个请求的最佳方法,以便该进程可以在后台运行并且调用可以重叠。

我喜欢BackgroundWorker提供的功能,并且我阅读了这里< /a> 它在幕后使用了 ThreadPool。我真的不明白如何利用它来达到我的目的。看来,如果我必须发送 1000 个查询,我可以做一个循环,在其中为每个查询调用一个新的 BG 工作线程,并且线程池一次将使用最多 25 个(?)线程,其余 975 个请求是排队。是这样吗?

如果这是正确的,我想排队 1000 个请求的过程本身会冻结 UI,那么排队循环本身是否应该在另一个 BG Worker 中运行?

从一个工作线程调用其他工作线程有问题吗?

我是否应该只创建 20 个 BG 工作线程,并在其中一个完成后手动启动另一个线程?

我这样理解对吗?任何建议将不胜感激!

Hi I need to send a WMI query to each system in a domain (potentially thousands), and WMI queries seem to take a long time to return. So I am reviewing the best ways to send multiple requests using multiple threads, so the process can run in the background and the calls can overlap.

I like the features that BackgroundWorker offers, and I read HERE that it uses the ThreadPool under the covers. I dont really understand though how I would leverage this to serve my purposes. It seems that if I had to send 1000 queries, I could do a loop in which I invoke a new BG worker for each query, and the threadpool will use up to 25(?) threads at one time, and the remaining 975 requests are queued. Is that what happens?

If this is right, I imagine the process of queuing up 1000 requests will itself freeze the UI, so should the queuing loop itself be running in another BG worker?

Is there a problem with invoking other worker threads from a worker thread?

Should I be only creating say 20 BG worker threads and manually launching another when one completes?

Am I understanding this right? Any advice would be much appreciated!

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

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

发布评论

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

评论(1

残花月 2024-12-04 15:47:33

我使用 System.Threading.Tasks 命名空间中的 Parallel.ForEach 方法。

创建一个列表包含您要查询的所有主机名。然后创建一个方法,该方法接受一个字符串作为输入并查询它,并执行您想要对该数据执行的任何操作。

将它们放入像这样的 ForEach 方法中

Parallel.ForEach(ComputerList, QueryAComputer);

并让它撕裂。确保在不需要 ManagementObjects 时立即调用它们的 Dispose() 方法。我认为当一次执行太多查询时,存在某种问题会导致 WMI 中断。 Dispose() 应该有助于释放这些资源并防止死锁。

I use the Parallel.ForEach method found in the System.Threading.Tasks namespace.

Make a List<string> containing all the host names you want to query. Then make a method that takes a string as it's input and queries it and does whatever it is you're wanting to do with that data.

Put them in a ForEach method like this

Parallel.ForEach(ComputerList, QueryAComputer);

and let it rip. Be sure to call the Dispose() method on ManagementObjects as soon as you don't need them. I think there's some kind of issue that causes WMI to break when too many queries are performed at once. Dispose() should help release those resources and prevent deadlock.

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