简体   繁体   English

任务在通话后几秒钟运行

[英]Task running few seconds after the call

I have a service that receive messages and sends them to an instance every message i receive i'm sending it to new Task so the operation of process the message will be async 我有一个接收消息的服务,将收到的每条消息发送给一个实例,然后将其发送给新Task,因此消息的处理操作将是异步的

public void ReceiveMessage(string Message) {
      Logger.Logger.Log($"Receive Message {Message} in METHOD method");  
      //see in the log time stamp 12:13:51.000
                Task.Factory.StartNew(() =>
                {
                    Logger.Logger.Log($"Receive Message  {Message} in TASK method"); 
                    //see in the log time stamp 112:13:53.000

                    processMessage(Message);
                });
}

private void processMessage(string message) {
     //do some processing job
}

there are a lot of messages received in the service from different clients , each message is send to different instance , the ReceiveMessage and processMessage are methods per instance So there are 2 seconds delay between calling the method and starting the task - does any one know why this could happens and how to avoid it? 服务中从不同的客户端收到很多消息,每条消息发送到不同的实例, ReceiveMessageprocessMessage是每个实例的方法,因此在调用方法和启动任务之间有2秒的延迟-有谁知道为什么这可能会发生,如何避免呢?

I thought C# is able to manage it's own thread pools... 我以为C#能够管理自己的线程池...

There is a limited number of Task that can run concurrently, check the property of Task.Factory.Scheduler.MaximumConcurrencyLevel 可以同时运行的Task数量有限,请检查Task.Factory.Scheduler.MaximumConcurrencyLevel的属性

The default scheduler for the Task Parallel Library and PLINQ uses the .NET Framework thread pool, which is represented by the ThreadPool class, to queue and execute work. 任务并行库和PLINQ的默认调度程序使用ThreadPool类表示的.NET Framework线程池来排队和执行工作。 The thread pool uses the information that is provided by the Task type to efficiently support the fine-grained parallelism (short-lived units of work) that parallel tasks and queries often represent. 线程池使用Task类型提供的信息来有效地支持并行任务和查询经常表示的细粒度并行性(短期工作单元)。

However the ThreadPool pool size depends on several factors: 但是, ThreadPool池的大小取决于几个因素:

There is one thread pool per process. 每个进程只有一个线程池。 Beginning with the .NET Framework 4, the default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. 从.NET Framework 4开始,进程的线程池的默认大小取决于几个因素,例如虚拟地址空间的大小。 A process can call the GetMaxThreads method to determine the number of threads. 进程可以调用GetMaxThreads方法来确定线程数。 The number of threads in the thread pool can be changed by using the SetMaxThreads method. 可以使用SetMaxThreads方法更改线程池中的线程数。 Each thread uses the default stack size and runs at the default priority. 每个线程使用默认堆栈大小,并以默认优先级运行。

TheadPool - https://msdn.microsoft.com/en-gb/library/system.threading.threadpool.aspx TheadPool- https: //msdn.microsoft.com/zh-CN/library/system.threading.threadpool.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM