关于SemaphoreSlim与异步/等待使用 [英] Regarding the usage of SemaphoreSlim with Async/Await

查看:1670
本文介绍了关于SemaphoreSlim与异步/等待使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个先进的开发者。我只是试图让的任务库,只是google搜索的保持。我从来没有使用过类 SemaphoreSlim ,所以我想知道它做什么。在这里,我present一个code,其中用于与 SemaphoreSlim 异步和放大器;等待但我不明白。所以,我在这里粘贴code。请,可有人帮我了解下code。

第1套$ C $的C

 等待WorkerMainAsync();异步任务WorkerMainAsync()
{
  SemaphoreSlim SS =新SemaphoreSlim(10);
  而(真)
  {
    等待ss.WaitAsync();
    //你可能需要的地方保存这个任务,然后等待它
    变种任务= DoPollingThenWorkAsync();
  }
}异步任务DoPollingThenWorkAsync(SemaphoreSlim信号)
{
  VAR味精=轮询();
  如果(MSG!= NULL)
  {
    等待Task.Delay(3000); //处理I / O密集​​型工作
  }  //这是假定你不必担心例外
  //否则考虑尝试,终于
  semaphore.Release();
}

首先, WorkerMainAsync 将被调用,有 SemaphoreSlim 被使用。为什么10传递到 SemaphoreSlim 的构造函数。

在没有控制再出来while循环?

这是什么 ss.WaitAsync();

DoPollingThenWorkAsync()函数需要一个 SemaphoreSlim ,但是当它被称为是不是通过什么... ....这是笔误?

为什么伺机Task.Delay(3000); 使用

他们可以简单地使用 Task.Delay(3000)但为什么他们使用等待这里吧。

第2集code为同一目的

 异步任务WorkerMainAsync()
{
  SemaphoreSlim SS =新SemaphoreSlim(10);
  清单<任务> trackedTasks =新的List<任务>();
  而(DoMore())
  {
    等待ss.WaitAsync();
    trackedTasks.Add(Task.Run(()=>
              {
                DoPollingThenWorkAsync();
                ss.Release();
              }));
  }
  等待Task.WhenAll(trackedTasks);
}无效DoPollingThenWorkAsync()
{
  VAR味精=轮询();
  如果(MSG!= NULL)
  {
    Thread.sleep代码(2000); //过程中长时间运行的CPU绑定工作
  }
}

下面是一个任务和放大器; ss.Release 添加到列表....我真的不明白,添加任务列表后,他们如何才能跑?

  trackedTasks.Add(Task.Run(异步()=>
              {
                等待DoPollingThenWorkAsync();
                ss.Release();
              }));

我期待着一个很好的解释和放大器;有助于了解两套code的。谢谢


解决方案

  

为什么10传递到SemaphoreSlim构造。


他们使用 SemaphoreSlim 来限制到一次10个任务。信号灯是采取每一个任务开始前,每个任务释放它,当它完成。欲了解更多关于信号灯,请参见 MSDN


  

他们可以使用简单的Task.Delay(3000),但他们为什么使用这里等待着。


Task.Delay 创建指定的时间间隔后完成并返回它的任务。大多数工作 -returning方法,如, Task.Delay 立即返回;它返回工作具有延迟。因此,如果code没有等待它,就不会有延迟。


  

只是真的不添加任务之后明白列出它们如何运行?


基于任务的异步模式工作对象返回热。这意味着他们已经通过他们回来的时候运行。在伺机Task.WhenAll 在最后等待他们全部完成。

I am not an advanced developer. I just try to get a hold on the task library and just googling. I never used the class SemaphoreSlim so I would like to know what it does. Here I present a code where SemaphoreSlim is used with async & await but which I do not understand. So I am pasting the code here. Please, could some one help me to understand the code below.

1st set of code

await WorkerMainAsync();

async Task WorkerMainAsync()
{
  SemaphoreSlim ss = new SemaphoreSlim(10);
  while (true)
  {
    await ss.WaitAsync();
    // you should probably store this task somewhere and then await it
    var task = DoPollingThenWorkAsync();
  }
}

async Task DoPollingThenWorkAsync(SemaphoreSlim semaphore)
{
  var msg = Poll();
  if (msg != null)
  {
    await Task.Delay(3000); // process the I/O-bound job
  }

  // this assumes you don't have to worry about exceptions
  // otherwise consider try-finally
  semaphore.Release();
}

Firstly, the WorkerMainAsync will be called and there a SemaphoreSlim is used. Why is 10 passed to the constructor of SemaphoreSlim.

When does the control come out of the while loop again?

What does ss.WaitAsync(); do?

The DoPollingThenWorkAsync() function is expecting a SemaphoreSlim but is not passed anything when it is called.......is this typo?

Why is await Task.Delay(3000); used?

They could simply use Task.Delay(3000) but why do they use await here instead.

2nd set of code for same purpose

async Task WorkerMainAsync()
{
  SemaphoreSlim ss = new SemaphoreSlim(10);
  List<Task> trackedTasks = new List<Task>();
  while (DoMore())
  {
    await ss.WaitAsync();
    trackedTasks.Add(Task.Run(() => 
              {
                DoPollingThenWorkAsync();
                ss.Release();
              }));
  }
  await Task.WhenAll(trackedTasks);
}

void DoPollingThenWorkAsync()
{
  var msg = Poll();
  if (msg != null)
  {
    Thread.Sleep(2000); // process the long running CPU-bound job
  }
}

Here is a task & ss.Release added to a list....I really do not understand that after adding tasks to a list how they can run?

trackedTasks.Add(Task.Run(async () => 
              {
                await DoPollingThenWorkAsync();
                ss.Release();
              }));

I am looking forward for a good explanation & help to understand the two sets of code. Thanks

解决方案

why 10 is passing to SemaphoreSlim constructor.

They are using SemaphoreSlim to limit to 10 tasks at a time. The semaphore is "taken" before each task is started, and each task "releases" it when it finishes. For more about semaphores, see MSDN.

they can use simply Task.Delay(3000) but why they use await here.

Task.Delay creates a task that completes after the specified time interval and returns it. Like most Task-returning methods, Task.Delay returns immediately; it is the returned Task that has the delay. So if the code did not await it, there would be no delay.

just really do not understand after adding task to list how they can run?

In the Task-based Asynchronous Pattern, Task objects are returned "hot". This means they're already running by the time they're returned. The await Task.WhenAll at the end is waiting for them all to complete.

这篇关于关于SemaphoreSlim与异步/等待使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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