如何在 C# 控制台应用程序中管理所有正在运行的线程?

发布于 2024-11-03 14:29:57 字数 236 浏览 4 评论 0原文

我在控制台应用程序中遇到管理线程并行问题。
我正在并行运行 10 个线程所有线程都在执行某些特定任务。
如果任何任务结束/完成,则执行停止/结束线程并立即启动新的线程实例。
我想要 10 个线程,这样任何线程都会停止/结束,然后它会生成新线程。
但每次我想要在控制台应用程序中有 10 个线程处于运行模式时
它应该是使用 C# 控制台应用程序并行工作。
如何在 C# 控制台应用程序中运行 10 个线程?

I having problem managed thread parallel in console application.
I am running 10 threads parallel & all thread doing some specific task.
In case if any task is over/completed then doing stop/end thread and immediate I started new thread instance.
I want 10 threads so anyone thread is going to stop/end then It generates new thread.
but every time I want 10 threads in running mode in console application &
It should be parallel work using C# console application.

How I can running 10 threads in C# console application?

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

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

发布评论

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

评论(2

甜味拾荒者 2024-11-10 14:29:57

在每个线程的末尾,在某个共享对象上放置一个锁(lock (obj) {})。
然后从您拥有的线程集合中删除当前线程。
如果 collection.Count 小于 10,则创建一个新集合并将其放入集合中。
释放锁。

private List<Thread> threads = new List<Thread>();

private void ThreadFunction() {
  // do something
  // here before the lock
  lock (threads) {
    threads.Remove(Thread.CurrentThread);
    if (thread.Count < 10) {
      Thread t = new Thread(ThreadFunction);
      threads.Add(t);
      t.Start();
    }
  }
}

请务必捕获线程内的所有异常,否则当线程异常发生时,您的代码将失败。也就是说,确保代码的锁定部分始终被调用(线程中止异常除外,但这并不重要)。

但正如我所说,我认为你应该使用 ThreadPool 来完成这样的任务......

At the end of each thread put a lock on some shared object (lock (obj) {}).
Then remove the current thread from a collection of threads you have.
If the collection.Count is less than 10 create a new one and put inside the collection.
Release the lock.

private List<Thread> threads = new List<Thread>();

private void ThreadFunction() {
  // do something
  // here before the lock
  lock (threads) {
    threads.Remove(Thread.CurrentThread);
    if (thread.Count < 10) {
      Thread t = new Thread(ThreadFunction);
      threads.Add(t);
      t.Start();
    }
  }
}

Be sure to catch all exception inside the thread or you code will fail when a thread exception happens. That is make sure that the lock part of the code is always called (except on a Thread abord exception but that will not matter).

But as stated I think you should use a ThreadPool for such a task...

阳光下慵懒的猫 2024-11-10 14:29:57

关于.Net中线程的书是:http://www.albahari.com/threading/
仅此一点就可能回答您的任何问题。

根据您使用这些线程的目的(我猜测您可能正在谈论在后台运行事务),您可能需要使用BackgroundWorker。

http://msdn.microsoft.com/en-us/library /system.componentmodel.backgroundworker.aspx

BackgroundWorker 仅允许您处理开始/结束/进度事件,从而使调试更不易出错。

The book on threads in .Net is: http://www.albahari.com/threading/
This alone will probably answer any questions you have.

Depending on what you are using these threads for (I am guessing that you may be talking about running transactions in the background) you may want to use BackgroundWorker.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

BackgroundWorker lets you deal with Begin/End/Progress Events only, making debugging much less error prone.

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