How to wait a certain amount of time between each execution of a method


All to all,

I have an application that consumes several web services. What this application does that gets many users and creates entries in a .csv file for several projects.

I would like to do the execution for a certain amount of users, every 100 or 200 users the application wait about 30 seconds and continue its execution.

Let this be done until the execution is complete.

I want to avoid loading Querys into the database and prevent the traffic from getting saturated because I make several web calls.

How can I do that?

Thank you very much in advance.

Here is part of my code:

UserData[] userIds = GetUserID();
if (userIds.Length > 0)

    List<string> multiUserIDs = new List<string>();

    foreach (var userdata in userIds)

    string testName = "test_";
    string outCsvFile = string.Format(@"D:\TasksForAllProjects\{0}.csv", testName + DateTime.Now.ToString("_yyyyMMdd HHmms")); 
    String newLine = "";
    var stream = File.CreateText(outCsvFile);


    string temp = "";

    var AllProjectIds = proj.ProjectID; //Esto viene de otra llamada hecha mas arriba

    string[] projectIDs = new string[] { AllProjectIds }; // all projects
    string singleUserID = "";
    string[] taskStatus = new string[] { "notcompleted" };  

    TaskEntry[] result = GetTasks(projectIDs, singleUserID, multiUserIDs.ToArray(),taskStatus); 

    newLine = string.Format("\"{0}\",\"{1}\",\"{2}\"",
        //Creating a new file when new entries are found.

I see that the problem could come from the variable multiUserIDs which has all the users.

Then I should execute a number of users that comes from that variable.

asked by A arancibia 29.04.2016 в 21:45

2 answers


If you are generating a csv, do not do it that way, use a library as being


then you can map the complete data without having to go through register by record.

I understand that you have a class in the variable result in that case the export is direct

var csv = new CsvWriter( textWriter );
csv.WriteRecords( result );

It's more if you analyze the documentation you could customize the mapping and generate the file in a single operation, so you would not have to use any code stop.

> > if I run the call most of the time I have a timeout or freeze the database

I do not see how you are recovering the records, but that problem is not the csv generation but how you recover the data, maybe you should evaluate using a DataReader

But stopping the code does not seem like a good alternative.

answered by 29.04.2016 / 22:00

You have to sleep the thread:


The parameter it receives is in Milliseconds.

Anyway, doing this sometimes is not a good idea, and you would have to ask yourself why you have to sleep the application for so long. If this is something that is going to be maintained over time you should consider another design.

answered by 29.04.2016 в 21:46