我有带有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.
发布评论
评论(1)
您的代码永远不会等待该任务结束。使用
等待
和task.run
而不是
线程。它们是由ThreadPoool线程执行的工作。创建线程是一个昂贵的操作。为了避免创建和破坏线程的成本,.NET保留了一组工人线程。以行动或功能代表的形式作业,将其发布到该线程池并由其中一个工作线程执行。
task.run
或task.factor.startnew
将作业张贴到线程池进行执行并返回task
,本质上是“ Promise”对象。这意味着通话线程不会阻止等待任务完成并可以完成其他工作。等待
使等待该任务可以完成并回到调用线程变得容易。任务完成后,等待
后,执行可以恢复代码。在桌面应用程序中,这意味着UI线程不会被阻止等待任务完成,并且可以继续处理Windows消息,按钮点击,刷新其窗口等。任务完成后,执行将在UI线程上恢复使用代码这是在
等待
之后。Your code never awaits that task to end. Use
await
andTask.Run
instead:or
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
orTask.Factor.StartNew
post a job to the threadpool for execution and return aTask
, 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 afterawait
.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
.