将对象传递给线程并在线程运行后将其返回

发布于 2024-11-04 18:50:08 字数 1019 浏览 1 评论 0原文

我会立即这么说。我是线程方面的业余爱好者。我是一名高级 c# Web 开发人员,但我有一个项目需要我填充大量对象,这些对象需要很长时间才能填充,因为它们需要 WebRequests>要填充的响应。我的一切都在没有线程的情况下工作,但它的运行速度不足以满足我的要求。我想将所有内容传递给 ThreadPool 来为我管理线程,因为出于显而易见的原因,我可能会同时排队 20,000 个线程。我不想访问一个带有立即填充所有请求所需的请求的网站。

我想做的是传入一个对象,填充它,然后将其添加到主线程中的集合中已有人居住。然后,填充所有对象后,继续执行程序。我不知道需要填充多少个对象,直到它们全部填充为止。

我的问题...执行此操作的最佳方法是什么?

这是我试图加速的循环:

foreach (HElement hElement in repeatingTag.RunRepeatingTagInstruction())
{
    object newObject = Activator.CreateInstance(currentObject.GetType().GetGenericArguments()[0]);
    List<XElement> ordering = GetOrdering(tagInstructions.Attribute("type").Value);
    RunOrdering(ordering, newObject, hElement);
    MethodInfo method = currentObject.GetType().GetMethod("Add");
    method.Invoke(currentObject, new[] { newObject });
}

我事先不知道该对象是什么,所以我使用激活器创建它。 RunOrdering 方法运行我传递的指令,告诉它如何填充对象。然后我将其添加到集合中。此外,对象本身可能具有需要此方法来运行并填充其数据的属性。

I will say this right off the bat. I am an amateur at threading. I am a senior c# web developer, but I have a project that requires me to populate a lot of objects that take a long time to populate as they require WebRequests and Responses to populate. I have everything working without threading, but it does not run fast enough for my requirements. I would like to pass everything to a ThreadPool to have the threading managed for me as I may be queuing up 20,000 threads at the same time and for obvious reasons. I do not want to hit a website with the requests needed to populate all of them at once.

What I would like to do is to pass in an object, populate it, and then add it to a collection in the main thread once it is populated. Then once all the objects are populated, continue on with execution of the program. I do not know how many objects will need to be populated until they are all populated either.

My question...What is the best approach to doing this?

Here is the loop that I am trying to speed up:

foreach (HElement hElement in repeatingTag.RunRepeatingTagInstruction())
{
    object newObject = Activator.CreateInstance(currentObject.GetType().GetGenericArguments()[0]);
    List<XElement> ordering = GetOrdering(tagInstructions.Attribute("type").Value);
    RunOrdering(ordering, newObject, hElement);
    MethodInfo method = currentObject.GetType().GetMethod("Add");
    method.Invoke(currentObject, new[] { newObject });
}

I don't know what the object is beforehand so I create it using the Activator. The RunOrdering method runs through the instructions that I pass that tell it how to populate the object. Then I add it to the collection. Also, the object itself may have properties that will require this method to run through and populate their data.

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

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

发布评论

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

评论(3

风情万种。 2024-11-11 18:50:08

由于您可能必须等待它们全部完成,因此您需要的只是一个 Parallel.ForEach() 或等效函数。和一个线程安全的集合。请注意,对于 I/O 密集型任务,您可能需要限制线程数。 20.00 个线程在任何情况下都是疯狂的。

但我们需要查看更多细节(代码)。请注意,不存在“主线程中的集合”之类的东西。

Since you probably have to wait for them all to be complete, all you need is a Parallel.ForEach() or equivalent. And a Thread-safe collection. Note that for I/O intensive tasks you would want to limit the number of Threads. 20.00 threads would be insane in any situation.

But we would need to see more details (code). Note that there is no such thing as "a collection in the main thread".

想你只要分分秒秒 2024-11-11 18:50:08

填充许多需要a的对象
需要很长时间才能填充
Web请求和响应

如果您正在执行请求,请避免线程化。
两个线程后没有加速,仅存在两个线程。
很多的麻烦都是徒劳的。

populate a lot of objects that take a
long time to populate as they require
WebRequests and Responses

Avoid Threading if you are doing requests.
No speedup after two threads, merely existent with the two.
A lot of truble for nothing.

太阳公公是暖光 2024-11-11 18:50:08

几个建议:

如果您使用 .net 4,请尝试使用 任务 。您可以更好地控制日程安排。尽量不要共享任何对象,使它们不可变以及所有警告和最佳实践 关于同步、共享数据等。

其次,您可能需要考虑一个进程外解决方案,例如 消息队列 (xMQ 产品或穷人的数据库表作为队列),因此如果需要,您将有机会将任务分配到多台计算机上。

Couple of suggestions:

If you are on .net 4 try using Tasks instead. You would have much better control over scheduling. Try to not share any objects, make them immutable and all the warnings and best practices about synchronisation, shared data etc.

And secondly you might want to think of an out of process solution like message queues (xMQ products or poor man's database table as queue) so you would have the chance to distribute your task over multiple machines if you need to.

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