last day (18 days later) » 

6:41 AM
hi SPorty
You may also check out this answer to get some clarity on async-await behavior stackoverflow.com/questions/60014173/…
 
Hey, Thanks. I will go through the link. I am trying to understand the behaviour but finding it a little bit confusing
 
Lets say you have a 3 Modules(Mod1,Mod2,Mod3)
Mod1 takes long time to execute and you don't want to lock up your main thread (UI) while this happens
You can solve this by Running mod1 in task, await Task.Run(()=>Mod1())
This will Run Mod1, while your UI thread is still free to render the display
 
7:08 AM
Scenario 2
Lets say you have a 3 Modules(Mod1,Mod2,Mod3)
And you want to execute the 3 modules concurrently (which is not same as asynchronously)
Then you can do something like this (it will also keep the UI Free)
List<Task> tasks = new List<Task>();

var t1 = Task.Run(() => Mod1());
var t2 = Task.Run(() => Mod2());
var t3 = Task.Run(() => Mod3());

tasks.Add(t1); tasks.Add(t2); tasks.Add(t3);

Task.WaitAll(tasks.ToArray());
Task.WaitAll() : Waits for all of the provided Task objects to complete execution.
@sporty_1993
what does this do in your code ?

` while (!quitThreadEvent.WaitOne(pollingInterval))`
 
This is to get inside loop after pollingINterval time
and when a method is async and await is there inside , what happens at await of that async method?
 
7:33 AM
ok
public async Task DoWork()
{


await Task1();

//Some more code here


}
public async Task Task1()
{
await Task.Delay(2000);
}
since we are awaiting Task1, Task1 will complete and only then control goes to //Some more code.
This ensures that your MainThread is not locked during execution of Task1
@sporty_1993
 
7:46 AM
await tempFunc(username, password).ConfigureAwait( false ); this is what Rufo mentioned so you are saying that it will wait here till tempFunc is fininshed and then only will move ahead
isn't that synchronous only?
 
no it is not, I think there is a confusion between concurrent operations and asynchronous, the latter will help keep the UI responsive, the asynchronous nature is by the fact that UI?MainThread is free
 
private static TimeSpan pollingInterval;
private static Task regenerationTask;
private static CancellationTokenSource quitThreadEvent;
private static ConcurrentQueue<KeyValuePair<string, string>> queue = new ConcurrentQueue<KeyValuePair<string, string>>();

internal static void Start()
{
pollingInterval = TimeSpan.FromSeconds(100);
quitThreadEvent = new CancellationTokenSource();
regenerationTask = ExecuteAsync( quitThreadEvent.Token );
}

internal static async Task ExecuteAsync(CancellationToken cancellationToken)
or it will run tempFunc in a thread and loopback through while again
 
yes, it will wait till tempfunc is done
btw why are you using ConfigureAwait(false) ?
 
I didn't, this is what Rufo added in thread.
 
Okay, when it comes to Async and Await there is just one renowned person in the Tech, Stephen Cleary
 
7:56 AM
Actually my usecase is that in service I want to run multipletasks and call the same method using those tasks with differnt arguments. and call will happen in every pollinginterval but there is a possibility that previous task is still running and still I want to run another task
 
check his response on : blog.stephencleary.com/2012/02/…
 
from what I read, I thought I can have Task.run do that
 
then why can't you split them into several tasks and use

Task.WaitAll(t1,t2,t3...);
and since you are using a service, which is UI agnostic
.ConfigureAwait(false) sounds reasonable
To summarize, By default, when you use async/await, it will resume on the original thread that started the request. However, if another long-running process currently has taken over that original thread, you will be stuck waiting for it to complete.
To avoid this issue, you can use a method called ConfigureAwait with a false parameter. When you do, this tells the Task that it can resume itself on any thread that is available instead of waiting for the thread that originally created it. This will speed up responses and avoid many deadlocks.
 
so there is a service which returns a list. I poll that service every 5 minutes and if something is returned I run the Task but at a time not more than 9 tasks can run. so what you suggested can't be used right?
because I am not aware of all the tasks running beforehand
 
you mean the relevance of Task.WaitAll() ? right
?
 
8:08 AM
yes
 
true
 
I want thinking that when I run async task, it will run that method and carry on with the loop
so that next time it can go ahead with creating another task
 
sorry did you mean **I was thinking ?
so there is a service which returns a list. I poll that service every 5 minutes and if something is returned I run the Task but at a time not more than 9 tasks can run. so what you suggested can't be used right?
you poll that service every 5 minutes and if it returns a list, u run a task
and can such a task take more than 5 minutes to complete ?
 
*Yeah
 
ahh
ok
 
8:15 AM
yeah it can take more than 5 minutes, that is why I want to run them parallely and there is a constraint that 9 tasks can run parallelly, that is the use of concurrentqueue, I mentioned 3 usernames but they are 9
 
but at every poll there will be atmost only 1Task that will be started new, is that ocrrect
 
yes
at everypoll there will be one task started
but if queue is empty that means 9 tasks are already running because at the end of the task, I enqueue the username password again
but if it isn't empty, that means there are lesser than 9 tasks
what Rufo wrote I think await tempFunc(username, password).ConfigureAwait( false ); is runnig at a worker thread
and while is at main thread
He wrote that "After the first await Task.Delay( ... ).ConfigureAwait( false ); we are on a worker thread"
 
0-5 m : 1Task Taking 10m
5-10m: 1 more Task taking 20
10-15m: 1 more Task taking 20
15-20: 1 More task taking 30m

So now 20m elapsed: 1 Task complete and 3 Task In Progress
Is this right ?
 
yes
and if queue has 9 users in the starting , it will has 8 now
*had
 
interesting, now I have a better high level understanding
 
8:20 AM
yeah, and that is running as a service
as a part of service those all tasks are running and doing something in tempFunc
but I still think The solution Rufo suggested is supposed to do what I am thinking
 
why don't you give it a dry run
 
I tried
but it just get stuck after Task.Delay
I am trying to debug, I am calling another service after that that is where it gets stuck
 
which line exactly ?
 
await Task.Delay( ... ).ConfigureAwait( false );
what configureAwait does exactly?
 
I think its the deadlock situation that people face with configureawait
that your facing now
I explained above
To summarize, By default, when you use async/await, it will resume on the original thread that started the request. However, if another long-running process currently has taken over that original thread, you will be stuck waiting for it to complete.
To avoid this issue, you can use a method called ConfigureAwait with a false parameter. When you do, this tells the Task that it can resume itself on any thread that is available instead of waiting for the thread that originally created it. This will speed up responses and avoid many deadlocks.
Why don't you attempt like this


Static Void Main(string [] args)
{
List<Task> taskList = new List<Task>();

PollEvery5Minute();


}
PollEvery5Minute()
{
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromMinutes(5);

var timer = new System.Threading.Timer((e) =>
{
Poll();
}, null, startTimeSpan, periodTimeSpan);


}

Poll()
{
//Ensures Task list is below 9
CheckTaskThreshold();

if(IsPollingSuccessfull())//If List is obtained
{

Task.Run(()=>//New Task);

//Add to List

}


}
 
8:41 AM
but again, I need to have something in service to for starting and stopping it.
may be , use the same which Rufo suggested for start and stop and try what you suggested
and does var timer = new System.Threading.Timer((e) =>
{
Poll();
}, null, startTimeSpan, periodTimeSpan);
make it polling every 5 minutes?
 
yes
"what do you mean something in service for starting and stopping"
you mean you need to get a list from the poll ?
 
no no.
internal static void Stop()
{
quitThreadEvent.Cancel();
regenerationTask.Wait();
}
for stopping the service using UI, this will be triggered
 
You can keep the start and stop service as it is, when started you can start the PollEver5Minute()
hope I made sense
 
9:22 AM
@sporty_1993, I'm leaving now
anything else you need ?
 
I will try as you suggested. Will contact it that doesn't work?
*if
 
alright, all the best
 
Thanks a lot
 
this is a very nice example to refer to
Bye for now
Feel free to edit my answer, as you find fit based on your investigations
@sporty_1993
 
 
5 hours later…
2:44 PM
hi @sporty_1993
hows it going ?
 
 
3 hours later…
5:15 PM
Hey @Clint, I used the new solution provided by Rufo, If you look at it , it is doing exactly what I wanted.
 
5:37 PM
hey @sporty_1993
Very well
Glad that it worked out finally
what does this mean _ = WorkWithAsync(username, password);
underscore ?
 

  last day (18 days later) »