none
Sleep for 10 Seconds every 5 employeeID RRS feed

  • Question

  • Hi - I am attempting to split my List into Chunks with the splitList function.  What my desired result is, I want to after each 5 employeeID display a message to the user that we are sleeping.  After 10 seconds process the next batch of 5 employeeID

    This is the code I have, but it is never sleeping?

    public static List<List<string>> splitList(List<string> locations, int nSize)
    {
    	var list = new List<List<string>>();
    
    	for (int i = 0; i < locations.Count; i += nSize)
    		list.Add(locations.GetRange(i, Math.Min(nSize, locations.Count - i)));
    
    	return list;
    }
    
    //important syntax
    List<string> empIDAll = new List<string>() { "j123", "a3232", "r1213", "z2323", "a23", "b2323", "r23", "z32", "m32", "l22" };
    var list = splitList(empIDAll, 5);
    int num = list.Count;
    
    foreach (var item in list)
    {
    	foreach (string i in item)
    	{
    		var t = HitFirstAPI(i, progress);
    		await HitSecondAPI(i, progress);
    		await t;
    	}
    	if (list.Count >= 5)
    	{
    		((IProgress<string>)progress).Report("Time To Sleep");
    		await Task.Delay(10000);
    	}
    }

    Tuesday, October 1, 2019 5:05 PM

Answers

  • Your code isn't going to work as is because your condition is that once the list has 5 or more items in it you sleep. The list count is set at the point you assign the value to it. So each time through the loop (assuming 5 or more items) then that condition is true and it delays. If you want to sleep every 5 iterations with a foreach then you have to track the # of iterations yourself. You can do this easily with a `for` loop since you can just look at the index. With `foreach` you'll need to track it yourself. 

    int iteration = 0;
    foreach (var item in list)
    {
       …
    
       if (++iteration == 5)
       {
          await Task.Delay(10000);
          iteration = 0;
       };
    };

    Alternatively you can partition your items into groups of 5 and then use a foreach to loop through each group followed by each item within the group but this requires changing your code even more.

    Of course this has nothing to do with actually displaying a prompt to the user when that happens. I assume you have already solved that problem.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by IndigoMontoya Wednesday, October 2, 2019 10:37 PM
    Wednesday, October 2, 2019 2:02 PM
    Moderator

All replies

  • Hello,

    See the following (you need to add the Delay to the code sample)

    https://devblogs.microsoft.com/dotnet/async-in-4-5-enabling-progress-and-cancellation-in-async-apis/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, October 1, 2019 5:19 PM
    Moderator
  • I don't see the link you provided using the Delay operatror?
    Tuesday, October 1, 2019 5:26 PM
  • I don't see the link you provided using the Delay operatror?
    Please read every word in my reply.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, October 1, 2019 6:35 PM
    Moderator
  • Hi IndigoMontoya,

    Thank you for posting here.

    For you question, the code is not completed.

    According to your description, a simple way is to make each 5 employeeID as a Task. And use Thread.Sleep(10000) to start the Task to process the next batch of 5 employeeID each 10 seconds. Before, Thread.Sleep(10000), you could display a message to the user that we are sleeping.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 2, 2019 4:48 AM
    Moderator
  • Your code isn't going to work as is because your condition is that once the list has 5 or more items in it you sleep. The list count is set at the point you assign the value to it. So each time through the loop (assuming 5 or more items) then that condition is true and it delays. If you want to sleep every 5 iterations with a foreach then you have to track the # of iterations yourself. You can do this easily with a `for` loop since you can just look at the index. With `foreach` you'll need to track it yourself. 

    int iteration = 0;
    foreach (var item in list)
    {
       …
    
       if (++iteration == 5)
       {
          await Task.Delay(10000);
          iteration = 0;
       };
    };

    Alternatively you can partition your items into groups of 5 and then use a foreach to loop through each group followed by each item within the group but this requires changing your code even more.

    Of course this has nothing to do with actually displaying a prompt to the user when that happens. I assume you have already solved that problem.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by IndigoMontoya Wednesday, October 2, 2019 10:37 PM
    Wednesday, October 2, 2019 2:02 PM
    Moderator