C
C#10mo ago
Zyrus

✅ 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:
-Start-
-Method A- Called x1
-Method A- Finishes x1
-Method B- Called x1
---Loops--
-Method A- Called x2
{there is chance -Method B- can finishes here}
-Method A- Finishes x2
Wait for (-Method B- Finish x1)
-Method B- finishes x1 {if it didn't Earlier}
--Loops--
-Method A- Called x3
{there is chance -Method B- can finishes here}
-Method A- Finishes x3
Wait for (-Method B- Finish x2)
-Method B- finishes x2 {if it didn't Earlier}
--Loops--
-Start-
-Method A- Called x1
-Method A- Finishes x1
-Method B- Called x1
---Loops--
-Method A- Called x2
{there is chance -Method B- can finishes here}
-Method A- Finishes x2
Wait for (-Method B- Finish x1)
-Method B- finishes x1 {if it didn't Earlier}
--Loops--
-Method A- Called x3
{there is chance -Method B- can finishes here}
-Method A- Finishes x3
Wait for (-Method B- Finish x2)
-Method B- finishes x2 {if it didn't Earlier}
--Loops--
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
frobnicate
frobnicate10mo ago
Place your await B before the next iteration of the loop
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Zyrus
ZyrusOP10mo ago
Here i Tried writing somethings simple :
static void Main(string[] args)
{

string[] rofiles = File.ReadAllLines(Profiles.txt);

foreach (var Profile in Profiles)
{
BrowserTask1Method(Profile);
Console.WriteLine("Task 1 for " + Profile + " Done!")

//I want to impliment Wait for task 2 of previus loop itteration here

BrowserTask2Method(Profile); //i want this to start runnning independently and continue to Running Task 1 for Next Profile
Console.WriteLine("Task 2 for " + Profile + " Done!")
}


}
static void Main(string[] args)
{

string[] rofiles = File.ReadAllLines(Profiles.txt);

foreach (var Profile in Profiles)
{
BrowserTask1Method(Profile);
Console.WriteLine("Task 1 for " + Profile + " Done!")

//I want to impliment Wait for task 2 of previus loop itteration here

BrowserTask2Method(Profile); //i want this to start runnning independently and continue to Running Task 1 for Next Profile
Console.WriteLine("Task 2 for " + Profile + " Done!")
}


}
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?
frobnicate
frobnicate10mo ago
foreach isn't async. You can do something like:
var taskList = new List<Task<MyResult>>();

foreach(...)
{
taskList.Add(Task.Run(() => PerformWhatever());
}

var result = await Task.WhenAll(taskList);
var taskList = new List<Task<MyResult>>();

foreach(...)
{
taskList.Add(Task.Run(() => PerformWhatever());
}

var result = await Task.WhenAll(taskList);
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 started
Zyrus
ZyrusOP10mo ago
yes, i meant that i wanted the Task2 in my case to be async, sorry for the misunderstanding.
frobnicate
frobnicate10mo ago
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
Zyrus
ZyrusOP10mo ago
yea looks like this is the best option thanks a ton!
Want results from more Discord servers?
Add your server