暂停循环直到内部函数返回 (C#)

发布于 2024-12-08 15:52:10 字数 529 浏览 0 评论 0原文

我有一个循环,在 LoadAmazonDataByBatch() 返回之前我不想继续下去。我知道必须有一种直接的方法来做到这一点,而且我几乎可以肯定我处理这个问题的方法是错误的。

const int batchSize = 500;
for (int i = 0; i < total; i = i + batchSize)
{
    LoadAmazonDataByBatch(i, batchSize, fileList, total, amazonLogHandler, stopWatch);
}

LoadAmazonDataByBatch() 在工作线程上执行一系列操作,包括创建一个临时数据集,如果没有批处理,该数据集会变得非常大。在处理和处置旧数据集(通过 LoadAmazonDataByBatch)之前,我不想创建新数据集。 显然,按照现在的写法,所有事情几乎都是同时发生的。

我怎样才能更好地处理这个问题?

I have a loop that I don't want to continue until LoadAmazonDataByBatch() has returned. I know there must be a straight forward way of doing it, and I'm almost certain I'm approaching the problem wrong.

const int batchSize = 500;
for (int i = 0; i < total; i = i + batchSize)
{
    LoadAmazonDataByBatch(i, batchSize, fileList, total, amazonLogHandler, stopWatch);
}

LoadAmazonDataByBatch() does a bunch of things on worker threads including creating a temporary DataSet that would get very large without the batching. I don't want to create a new DataSet until the old one is processed and disposed (by LoadAmazonDataByBatch).
Obviously the way this is written now everything happens almost all at once.

How can I approach this better?

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

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

发布评论

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

评论(5

┼── 2024-12-15 15:52:10

您需要进行某种线程同步。

不清楚您在哪里获得 LoadAmazonByBatch() ,但我建议

  • 检查该函数的文档以查看是否有该操作的同步版本。

  • 如果没有可用的文档,那么您将需要卷起袖子。它可能需要查看或修改 LoadAmazonByBatch() 的源代码。查找由工作人员完成时设置的 ManualResetEvent。或者,也许该方法完成时会发出一个常规 .NET 事件。如果这些东西不存在,你需要添加类似的东西。

You need to do some sort of thread synchronization.

Not clear where you got the LoadAmazonByBatch() , but I'd suggest

  • checking the doc for that function to see if there is a synchronous version of the operation.

  • if no doc is available, then you will need to roll up yr sleeves. It may require viewing or modifying the source of LoadAmazonByBatch(). Look for a ManualResetEvent that is set by the workers when they are finished. Or, maybe there is a regular .NET event that is emitted by that method when it completes. If those things don't exist you'll need to add something like that.

情仇皆在手 2024-12-15 15:52:10

LoadAmazonDataByBatch 很可能会创建一堆线程。您必须对所有创建的线程调用 Join 才能等待它们完成。

It's very likely that LoadAmazonDataByBatch creates a bunch of threads. You have to call Join on all created threads to wait till they complete.

几度春秋 2024-12-15 15:52:10

当然,这个不会等待函数返回的唯一方法是异步编写吗?

相关代码不是您发布的循环,而是我们需要查看的 LoadAmazonDataByBatch() 的定义。

Surely the only way this wouldn't wait for the function to return is if it's written asynchronously?

The relevant code isn't the loop you posted, it's the definition of LoadAmazonDataByBatch() that we need to see.

挽心 2024-12-15 15:52:10

如果该函数有回调(stopWatch?),也许您可​​以在回调中调用该函数(LoadAmazonDataByBatch)。

If that function has a callback (stopWatch?), perhaps you could call the function (LoadAmazonDataByBatch) within the callback.

清浅ˋ旧时光 2024-12-15 15:52:10

如果 LoadAmazonDataByBatch() 生成子线程,并且它会一直运行直到每个线程完成,您可以使用 Thread.Join() 方法使其等待子线程要完成的线程。我不确定这对多个孩子来说如何,但我认为应该没问题。

参考:C# 中的线程

If LoadAmazonDataByBatch() generates child threads, and it runs until each of those threads is finished, you can use the Thread.Join() method to make it wait for the child threads to finish. I am not sure how that would work for multiple children but I think it should be OK.

Reference: Threading in C#

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