【问题标题】:How to add a pause between executing tasks in C#如何在 C# 中的执行任务之间添加暂停
【发布时间】:2015-08-27 01:08:13
【问题描述】:

我目前正在编写一个程序,该程序需要我在执行任务之间暂停。

所以我有 4 样东西。

  1. 读取限制
  2. 每次读取之间的延迟
  3. 总读取次数
  4. 全局延迟(任务完成后程序暂停'x'秒)

基本上,一项任务被视为“读取限制”。因此,例如,如果我有这些设置:

  1. 读取限制 (10)
  2. 每次读取之间的延迟 (20)
  3. 总读取数 (100)
  4. 全局延迟 (30)

程序必须根据“读取限制”从文件中读取 10 行,并且在读取每一行之间,基于“每次读取之间的延迟”有 20 秒的延迟。读取 10 行后,根据“全局延迟”暂停 30 秒。当全局延迟结束时,它会从停止的地方重新开始并继续执行此操作,直到根据“Total Reads”达到 100 的限制。

我尝试过使用System.Threading.Thread.Sleep(),但无法成功。我怎样才能用 C# 实现这一点?

提前致谢。

//用我的一些代码更新。

我这样加载文件:

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);
    }
}

我有 4 个全局变量:

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

我想把函数做成这样:

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++;
    }
}

【问题讨论】:

  • 如果您使用单词“任务”作为Task - 比Task.Delay 是您正在寻找的......但很难看出它是否真的与您的问题有关 - 考虑发布演示问题的小代码示例(以及“无法使其工作”部分)
  • @AlexeiLevenkov 我已经用一些代码更新了帖子,我不确定Task 是否会在这里帮助我,我之前从未在我的任何项目中使用过Task。跨度>

标签: c# multithreading timer


【解决方案1】:

这可能很有趣 - 这是使用 Microsoft 的响应式框架 (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 */
        });

如果您需要在阅读自然完成之前停止阅读,只需致电subscription.Dispose();

【讨论】:

  • 感谢您的回答,非常感谢。我来看看 MS 的反应式框架。
【解决方案2】:

你是什么意思

我尝试过使用 System.Threading.Thread.Sleep() 但我无法让它工作

这是一个实现您使用 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);
            }
        }
    }
}

【讨论】:

  • 我没有足够的经验,无法让它工作,对此感到抱歉!我已经测试了您的代码,它完全符合我的需要,非常感谢您发布示例。高度赞赏。
猜你喜欢
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 2018-03-24
  • 2011-07-09
相关资源
最近更新 更多