【问题标题】:Thread.Sleep() doesn't work in application Console C#Thread.Sleep() 在应用程序控制台 C# 中不起作用
【发布时间】:2015-04-07 20:20:35
【问题描述】:
class Program
{
    static void Main(string[] args)
    {
        const string PATH = @"C:\My_PATH\";
        const string FILE_NAME = "data_acquistion2";
        const string DATETIME_STOP_RECORD = "01-04-15 17:18";
        bool fichierNonExistant = false;
        PerformanceCounter cpuCounter;
        PerformanceCounter ramCounter;` 

        cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";

        ramCounter = new PerformanceCounter("Memory", "Available MBytes");

        string actualPeriod = "";
        if (!File.Exists(PATH + FILE_NAME + ".csv"))
        {
            FileStream myFs = File.Create(PATH + FILE_NAME + ".csv");
            fichierNonExistant = true;
            myFs.Close();

        }

        StreamWriter myWriter = new StreamWriter(PATH + FILE_NAME + ".csv", true);
        if (fichierNonExistant == true)
        {
            myWriter.WriteLine("CPU Used (%)" + "," + "RAM Free (%)" + "," + "Hour and record Date");
        }

        while (actualPeriod != DATETIME_STOP_RECORD)
        {
            actualPeriod = DateTime.Now.ToString("dd/MM/yy HH:mm:ss");
           // Console.WriteLine(periodeActuelle);
            myWriter.WriteLine(cpuCounter.NextValue() + "," + ramCounter.NextValue() + "," + actualPeriod);
           Thread.Sleep(20000); //If I add this the program doesn't write in the csv file
        }

    }


}`

嗨,

我在 C# 中遇到 Thread.Sleep 的问题,我开发了一个代码,用于将 % CPU(已使用)和 RAM 直接写入 csv 文件。

它可以毫无延迟地工作,但我想每 20 秒写入一次这个值,这就是我需要使用 Thread.Sleep(20000) 的原因。 我也试过Task.Delay,我也有同样的问题。

【问题讨论】:

  • 什么是“不起作用”和“问题就在这里”是什么意思发生了什么?你期望会发生什么?
  • 不相关,但我发现它不太可能退出,因为!= 比较字符串; IMO 你应该将actualPeriod 保留为DateTime 并使用<
  • DATETIME_STOP_RECORD 不包括秒,但actualPeriod 包括。所以这将永远循环。此外,根据您当前的文化,-/ 之间也可能存在差异。最好的办法是听 Marc 并比较 DateTimes。
  • Marc Gravell 是对的。将"dd/MM/yy HH:mm:ss""01-04-15 17:18" 进行比较会让你陷入无限循环...
  • @Ironboy07 听起来只是一个冲洗问题...您是否尝试过使用 File.AppendText 而不是保持作家开放?

标签: c# timeout console-application


【解决方案1】:

问题不在于 Thread.Sleep()。您正在使用 StreamWriter,并且由于您的程序可能永远不会关闭,因此您需要在写入后刷新 StreamWriter。

添加

myWriter.Flush() 在 Thread.Sleep() 之前

【讨论】:

  • 完美运行!感谢您的帮助 Shar1er80 :)
  • 不客气。检查您的 while 循环,正如其他人所提到的,它看起来是无限的,请检查我的答案。 :-)
  • 好的,我去看看。谢谢。
【解决方案2】:

问题是如果我添加 Thread.Sleep() 程序会在 csv 文件中写入任何内容。

(我假设那里缺少“不”)

这听起来像是一个冲洗问题;您的更改仍在编写器中缓冲。您可以尝试使用myWriter.Flush();,但请注意,您可能仍然会遇到共享文件访问等问题。因为您将暂停 20 秒(对计算机来说很长时间),您最好在不使用文件时将其关闭。 完全去掉作者会更有效,并在必要时简单地使用File.AppendText(path, newLine),注意在字符串中包含您选择的行尾。然后文件将几乎一直关闭。

另外:您的循环退出条件需要注意;现在它将永远退出它自己的选择(因为actualPeriod 包括秒,而DATETIME_STOP_RECORD 不包括)。最好也使用DateTime<(您可以从时间之前转到时间之后,而无需精确到时间)。

【讨论】:

  • 完美运行!感谢您的解释。好的,我也会改变我的循环。我现在明白它是如何工作的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多