I have a C# console application in which I am looping through list of users and creating each making an entry into another database.
If the user creation has failed for a UserId
then I need to try again for the same user for a maximum of 5 times, between each try I need to wait for 10 seconds..
For this wait purpose I am using System.Threading.Thread.Sleep
foreach(var user in Users)
{
counter = 0;
CreateUserDetails(user.UserId);
if(!userCreatedSuccesfully)
{
do
{
counter++;
System.Threading.Thread.Sleep(10000);
CreateUserDetails(user.UserId);
}
while(userCreatedSuccesfully == false && counter <5)
}
}
I would like to replace Thread.Sleep
with WaitHandle
. How do I leverage WaitHandle
in my scenario here? Please suggest if there is any other better approach instead of WaitHandle
.
Note : I have posted only partial code here with only what is required.