i am not advance developer. just trying to have hold on task library just googling.i never use this class SemaphoreSlim so i like to know what it does. here i go through a code where SemaphoreSlim is used with Async & Await but do not understand because
i am not very advance developer. so here i am pasting the code. if possible please some one help me to understand the below code.
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();
}
first WorkerMainAsync will be called and there SemaphoreSlim is used. why 10 is passing to SemaphoreSlim constructor.
again when control goes out from while loop.
what ss.WaitAsync(); does ?
`DoPollingThenWorkAsync()` function expecting `SemaphoreSlim` but not passing anything when it is called.......is it typo ?
why they use await Task.Delay(3000);
they can use simply Task.Delay(3000) but why they use await here.
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 task & ss.release is added to list....just really do not understand after adding task to list how they can run?
trackedTasks.Add(Task.Run(async () =>
{
await DoPollingThenWorkAsync();
ss.Release();
}));
looking for good explanation & help to understand two set of code.thanks
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();
}
first WorkerMainAsync will be called and there SemaphoreSlim is used. why 10 is passing to SemaphoreSlim constructor.
again when control goes out from while loop.
what ss.WaitAsync(); does ?
`DoPollingThenWorkAsync()` function expecting `SemaphoreSlim` but not passing anything when it is called.......is it typo ?
why they use await Task.Delay(3000);
they can use simply Task.Delay(3000) but why they use await here.
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 task & ss.release is added to list....just really do not understand after adding task to list how they can run?
trackedTasks.Add(Task.Run(async () =>
{
await DoPollingThenWorkAsync();
ss.Release();
}));
looking for good explanation & help to understand two set of code.thanks