✅ Help Regarding async Method Implementation.
Hi, I'm a beginner trying to automate some tasks using c# and selenium but i need help with using async methods.
So, the scenario is, I have a foreach loop that runs 2 Methods (say A and B) a certain amount of times for each item in a list.(Method A and B cant run at the same time)
I want it go as follows:
And so on....
Basically, i want Method B to start independently from Main and wait for it to finish before running it again on the next loop.
How can I accomplish this?
Any guidance would be appreciated :D
I also cant figure out how can i use await inside the method since the browser task has various steps.
7 Replies
Place your
await B
before the next iteration of the loopUnknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Here i Tried writing somethings simple :
The loop is one block, and it runs over and over, how can I place something before next iteration? it will also be called before it all starts, no?
foreach
isn't async.
You can do something like:
Or you can use Parallel.Foreach
https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop
Also I'm not sure it makes sense to wait for a task before it has startedyes, i meant that i wanted the Task2 in my case to be async, sorry for the misunderstanding.
Then you can start both tasks, and await task2 at the bottom of the loop. In that way you're waiting for it "before the next iteration". If your task1 is also async, you need to await it somewhere btw
yea looks like this is the best option
thanks a ton!