简体   繁体   中英

How to add a pause between executing tasks in C#

I am currently writing a program which requires me to have a pause between executing tasks.

So I have 4 things.

  1. Read Limit
  2. Delay Between Each Read
  3. Total Reads
  4. Global delay (pause the program for 'x' seconds after a task is finished)

Basically, one task is considered the "Read Limit". So, for example, if I have these settings:

  1. Read Limit (10)
  2. Delay Between Each Read (20)
  3. Total Reads (100)
  4. Global Delay (30)

The program has to read 10 lines from the file based on "Read Limit" and between reading each line, there is a delay of 20 seconds based on "Delay Between Each Read". After it reads 10 lines, it is paused for 30 seconds based on "Global Delay". When the global delay is over, it starts again where it stopped and continues doing this until the limit of 100 is reached based on "Total Reads".

I have tried using System.Threading.Thread.Sleep() but I couldn't make it work. How can I achieve this with C#?

Thanks in advance.

//update with some of my code.

I load the file like this:

private void btnLoadFile_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string[] lines = System.IO.File.ReadAllLines(ofd.FileName);
    }
}

I have 4 global variables:

public int readLimit = 0;
public int delayBetweenRead = 0;
public int totalReads = 0;
public int globalDelay = 0;
public int linesRead = 0;

And I want to make the function like this:

private void doTask()
{
    while (linesRead <= readLimit)
    {
        readLine(); // read one line
        doDelay(); // delay between each line
        readLine(); // read another line and so on, until readLimit or totalReads is reached
        globalDelay(); // after readLimit is reached, call globalDelay to wait
        linesRead++;
    }
}

This might be of interest - here's the way to do this with Microsoft's Reactive Framework (NuGet "Rx-Main").

int readLimit = 10;
int delayBetweenRead = 20;
int globalDelay = 30;
int linesRead = 100;

var subscription =
    Observable
        .Generate(0, n => n < linesRead, n => n + 1, n => n,
            n => TimeSpan.FromSeconds(n % readLimit == 0 ? globalDelay : delayBetweenRead))
        .Zip(System.IO.File.ReadLines(ofd.FileName), (n, line) => line)
        .Subscribe(line =>
        {
            /* do something with each line */
        });

If you need to stop the reading before it finishes naturally just call subscription.Dispose(); .

What you do you mean by

I have tried using System.Threading.Thread.Sleep() but I couldn't make it work

Here is an example of achieving what you described with Thread.Sleep:

using (var fs = new FileStream("C:\\test.txt", FileMode.Open, FileAccess.Read))
{
    using (var sr = new StreamReader(fs))
    {
        int nRead = 0;
        while (nRead < settings.Total)
        {
            for (int i = 0; i < settings.ReadLimit && nRead < settings.Total; ++i, nRead++)
            {
                Console.WriteLine(sr.ReadLine());
                if (i + 1 < settings.ReadLimit)
                {
                    Thread.Sleep(settings.Delay * 1000);
                }
            }
            if (nRead < settings.Total)
            {
                Thread.Sleep(settings.GlobalDelay * 1000);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM