一段时间后我的线程从线程列表中消失

发布于 2025-01-29 04:05:52 字数 944 浏览 1 评论 0 原文

我有带有C#语言的多线程程序。 当加载主表单时,4个线程启动所有它们都与一起使用,而(true) 每个线程运行,活动且存在于线程列表中 即使断点不起作用。 我想从TCP和Process 3个步骤中读取数据,然后保存到数据库

  • 任务1:HAM while(true)并从TCP读取数据,然后添加到BlockingCollection BK1
  • 任务2:IN while(true)< /code>,从BK1中获取数据并处理数据,然后将数据添加到 blockingCollection bk2
  • task3 in in while(true)从bk2中获取数据并处理bk3
  • Task 4 in while(true)从BK3中获取数据,然后插入数据库

I定义任务:

Task.Factory.StartNew(() => myfunction, CancellationToken.None,
    TaskCreationOptions.PreferFairness, TaskScheduler.Default);

当单击按钮1开始并在一段时间后所有任务启动并正常工作时,任务3将状态更改为 rantococtertion ,并且不起作用i没有在代码中使用异步和等待,因为任务可行,不需要等待其他任务。

甚至 taskercreationoptions 设置为 Longrunning 我的功能具有,而(true)和工作生产者 - 消费者方法。

请帮助我解决问题。
感谢您的关注。

I have multi thread program with C# language.
When load main form, 4 thread start that all of them work with while(true)
Every thread run, active and exist in thread list but after 30 ms, one of thread (without any error) disappear from thread list and doesn't work
even breakpoint not work.
I want read data from TCP and process 3 steps then save to database

  • Task1: have while(true) and read data from tcp and add to blockingcollection bk1
  • Task 2: in while(true), take data from bk1 and process data then add data to BlockingCollection bk2
  • Task3 in while(true) take data from bk2 and process then bk3
  • Task 4 in while (true) take data from bk3 then insert database

I define task:

Task.Factory.StartNew(() => myfunction, CancellationToken.None,
    TaskCreationOptions.PreferFairness, TaskScheduler.Default);

When click button1 all of tasks start and work correctly after some time task3 change status to RanToCompletion and does not work I didn't use async and await in code because task works parallel and don't need wait for other task.

Even TaskCreationOptions set to LongRunning
My function have while(true) and work producer-consumer method.

Please help me about problem.
Thanks for attention.

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

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

发布评论

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

评论(1

看海 2025-02-05 04:05:52

您的代码永远不会等待该任务结束。使用等待 task.run

await Task.Run(()=>myFunction());

不是

await Task.Run(myFunction);

线程。它们是由ThreadPoool线程执行的工作。创建线程是一个昂贵的操作。为了避免创建和破坏线程的成本,.NET保留了一组工人线程。以行动或功能代表的形式作业,将其发布到该线程池并由其中一个工作线程执行。

task.run task.factor.startnew 将作业张贴到线程池进行执行并返回 task ,本质上是“ Promise”对象。这意味着通话线程不会阻止等待任务完成并可以完成其他工作。 等待使等待该任务可以完成并回到调用线程变得容易。任务完成后,等待后,执行可以恢复代码。

在桌面应用程序中,这意味着UI线程不会被阻止等待任务完成,并且可以继续处理Windows消息,按钮点击,刷新其窗口等。任务完成后,执行将在UI线程上恢复使用代码这是在等待之后。

async void btn1_Click(object sender,EventArgs args)
{
    var customerName=txtCustomer.Text;
    var orders=await Task.Run(()=>LoadOrdersFromDbAsync(customerName));
    grdOrders.DataSource=orders;

}

Your code never awaits that task to end. Use await and Task.Run instead:

await Task.Run(()=>myFunction());

or

await Task.Run(myFunction);

Tasks aren't threads. They're a job that gets executed by a threadpoool thread. Creating a thread is an expensive operation. To avoid the cost of creating and destroying threads, .NET keeps a pool of worker threads. Jobs, in the form of Action or Func delegates, get posted to that ThreadPool and executed by one of the worker threads.

Task.Run or Task.Factor.StartNew post a job to the threadpool for execution and return a Task, essentially a "promise" object. This means the calling thread isn't blocked waiting for the task to complete and can do other work. await makes it easy to await for that task to complete and get back to the calling thread. Once the task completes, execution can resume with the code after await.

In a desktop application that means the UI thread doesn't get blocked waiting for a task to complete and can keep processing Windows messages, button clicks, refresh its windows etc. When the task completes, execution will resume on the UI thread with the code that comes after await.

async void btn1_Click(object sender,EventArgs args)
{
    var customerName=txtCustomer.Text;
    var orders=await Task.Run(()=>LoadOrdersFromDbAsync(customerName));
    grdOrders.DataSource=orders;

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