是否可以在 dotnet 中引入多线程而不显式创建新线程?

发布于 2024-10-21 04:46:04 字数 512 浏览 5 评论 0原文

我有一个包含数百个需要处理的项目的循环。

通过有条件地设置全局 SQLConnection 来处理每个项目,其中使用此 SQLConnection 作为处理的一部分来处理该项目。

因此,不允许并行处理这些项目至关重要。

我意识到这不是一个好的设计,我希望尽快纠正它。

然而,尽管我尽了最大努力,这段代码似乎还是遇到了某种形式的多线程。不知何故,其中一项任务引发了异常。

此异常违反了外键约束,但表明它正在针对没有业务连接的 SQLConnection 进行操作。

当然,我对此有所担忧,但是据我所知,此应用程序中没有多线程代码。

我想知道是否可以在不显式创建新线程的情况下引入多线程

编辑:

  • VB.Net 3.5SP1
  • 控制台应用程序+类库
  • 偶尔调用Web服务
  • 使SQL调用
  • 没有太多其他内容。没有 Winform,就没有 WPF。

I have a loop of several hundred items which need to be processed.

Each item is processed by conditionally setting a global SQLConnection where upon the item is processed using this SQLConnection as part of the processing.

For this reason it is vital that none of these items is allowed to be processed in parallel.

I appreciate that this is not good design and I hope to rectify it as soon as is practical.

However it would seem that despite my best efforts, this code is experiencing some form of multi-threading. Somehow one of these tasks has thrown an exception.

This exception is the violation of a foreign key constraint, but indicates that it was operating against a SQLConnection which it has no business connecting to.

Naturally I have concerns about this, however to my knowledge there is no multi threading code in this app.

I wonder Is it possible to introduce multi threading without explicitly creating new threads

EDIT:

  • VB.Net 3.5SP1
  • Console App + Class Libraries
  • Occasionally Calls out to web services
  • Makes SQL calls
  • not much of anything else. No Winforms, no WPF.

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

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

发布评论

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

评论(4

椵侞 2024-10-28 04:46:04

是的 - 使用 System.Timers.Timer 和/或 System.Threading.Timer 可能会导致您描述的效果。每当计时器滴答作响时,新的工作项就会在线程池中排队 - 因此本质上您有一个多线程程序,而无需显式创建新线程。

如果计时器是自动重置(在调用 elapsed 后保持启用状态),则可能会同时导致对同一处理程序的另一次调用。

Yes - using System.Timers.Timer and/or System.Threading.Timer can cause the effect your describing. Whenever a timer ticks a new work item is queued in the ThreadPool - so essentially you have a multi threading program without explicitly creating new threads.

If the timer is AutoReset (remains enabled after elapsed has been called) you might cause another call to the same handler concurrently.

只等公子 2024-10-28 04:46:04

除了已经提到的其他内容之外:并行扩展(PLINQ 和任务并行库)。

In addition to the others that have been mentioned: parallel extensions (PLINQ and task parallel library).

梦巷 2024-10-28 04:46:04

另外,任务(即任务对象)不称为线程,而是线程。任务通常出现在 lambda 表达式附近,检查是否有。

哦,还有异步套接字和所有其他异步 IO。

但是:

与其不惜一切代价试图避免多线程,锁定不是更容易吗?抱歉,如果这个问题很幼稚,我可能会错过一些东西。

Alternatively tasks (ie Task objects) are not called threads, but are. Tasks are commonly found near lambda expressions, check if you have any.

Oh, and async sockets too and all the other async IOs.

BUT:

Instead of trying to avoid multithreading at all cost, wouldn't it be easier to lock ? Sorry if the question is naive, I may miss something.

成熟稳重的好男人 2024-10-28 04:46:04

您的代码是否是从第三方库调用的?通过使用事件,另一个库可以从任意多个线程调用您的代码。
我建议您检查调用更改的代码的代码,并确保不存在对您的代码的可疑调用。

Could it be that your code is called from a 3rd party library. By using events another library can call your code - from as many threads as it like.
I suggest you check the code that invoke the code that changes and make sure that there's no suspicious calls to your code.

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