如何在C#中创建任务或线程并在需要时运行它 [英] How to create a task or thread in C# and run it when needed

查看:472
本文介绍了如何在C#中创建任务或线程并在需要时运行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我每秒都会从特定的ip中收到一些值。我必须对那些花费超过一秒的值进行一些操作。如果我在这个操作上花了一秒钟,我将无法读取在那一秒期间生成的值,我将错过这些值。因此,我计划在C#中创建一个Task,并在每次获取值并将值传递给它时调用它,以便它运行操作,我不会错过任何值。

我正在执行以下操作

Hi,In my program I receive some values every second from a particular ip. I have to do some operation on those values which takes more than a second. If I spend a second on this operation, I would not be able to read the values generated during that second and I would miss out those values. So I am planning to create a Task in C# and invoke it whenever I get the values and pass the values to it so that it would run the operation and I would not miss out any values.
I was doing the following

if(array_size)<500
{       
Task.Run(() => SendMessage(testarray));
}



但我认为每次调用它都会创建新任务,几个小时后我就会出现内存异常。

你们可以请一些方法以有效的方式解决这个问题,这样我就不会遇到内存不足的错误。另外,如果可能的话,请分享一下解决方案的片段。

谢谢



我尝试了什么:



我试过运行它,几个小时之后,它会因内存不足而崩溃


But I think this is creating new task every time I invoke it and after few hours I got out of memory exception.
Could you guys please suggest me some way to solve this problem in an efficient way so that I don't encounter out of memory error. Also, if possible please share a snippet of the solution.
Thanks

What I have tried:

I tried running it and after few hours ,it would crash with out of memory exception

推荐答案

那个从长远来看,这不会解决您的问题:即使使用线程系统,如果您的处理需要1.1秒而且每1.0秒收到一次新数据,那么您将在处理器的秒数内完成0.1倍的核心数在你再次丢失数据之前充其量



线程无法解决这个问题,实际上它可能会导致问题更糟糕的是因为每个线程在内存和处理时间方面增加了额外的开销,使你的任务变得复杂。



你需要仔细查看你正在做的处理工作。提高效率或用更有效的算法取代它,这样你的处理需要0.9秒,而不是试图o ffload将问题转移到线程上。



抱歉,我们不能为你做这件事。
That won't fix your problem in the long run: even with a threaded system, if your processing takes 1.1 seconds and you receive new data every 1.0 seconds, you will last 0.1 times the number of cores you have in your processor seconds at best before you are losing data again.

Threading can't solve this, in fact it may make the problem worse because each thread adds extra overhead in terms of memory and processing time that complicates your task.

You need to look very carefully at the processing job you are doing and either improve its efficiency or replace it with a more efficient algorithm so that your processing takes 0.9 seconds, not try to "offload" the problem onto threads.

Sorry, but we can't do that for you.


你可以使用taskname 。 IsCompleted ,然后处理任务,请参阅:如何:等待一个或多个任务完成 [ ^ ]

示例:

You could use taskname.IsCompleted, and then dispose the task, see: How to: Wait on One or More Tasks to Complete[^]
Example:
Task checkProcesses = Task.Factory.StartNew(this.CheckProcesses);
//... e.g. a loop ...
While (True)
{
  if (checkProcesses.IsCompleted)
  {
	checkProcesses.Dispose();
	checkProcesses = Task.Factory.StartNew(this.CheckProcesses);
  }
}

private void CheckProcesses()
{
}


这篇关于如何在C#中创建任务或线程并在需要时运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆