代码之家  ›  专栏  ›  技术社区  ›  CrazyCoder

计算Windows服务C执行的任务类型#

  •  0
  • CrazyCoder  · 技术社区  · 6 年前

    我有一个使用C开发的Windows服务。这个服务一直在查找一个特定的文件夹路径,比如 inputfolder ,如果添加了任何新的文本文件,它将提取该文件,对其进行处理并生成输出,然后将其写入另一个日志文件,并从 输入文件夹 .

    现在的数据在 输入文件夹 可以是不同类型的,比如 TypeA TypeB 例如。此类型在读取文本文件中的数据后已知。每个文件都有 类型 类型B 不是两者都有。我可以读取txt文件并获得文件类型。

    我的要求是在一天结束的时候,我应该能告诉你有多少人 类型 任务已运行以及有多少任务 类型B 任务已运行。为此,我正在考虑执行以下操作之一:

    1. 声明一个静态变量,比如 countA countB 在应用程序中,一旦我得到类型,就增加适当的变量。但如果出于某种原因,服务必须在一天之间停止并启动,我将释放数据。

    2. 把这些细节写进一个单独的文件,比如txt或日志文件。首先读取文件,获取现有的计数,增加它并再次覆盖新的值。

    有人能提出建议吗?这是最好的方法。如果有人认为上述方法非常粗糙,请指导/建议我更好的方法。

    非常感谢!

    2 回复  |  直到 6 年前
        1
  •  0
  •   Mojtaba Tajik    6 年前

    正如你所说,这取决于你的要求。如果计数器值很重要,那么很可能由于服务中的任何错误而丢失主题,您应该在本地存储最新的计数器值。

    对于如何和在何处存储计数器值的场景,有多种选择:

    • 将计数器存储在磁盘上的文件中并保持文件最新
    • 将计数器存储在注册表项中

    您的代码应该是这样的:

     public static void Main()
        {
            var result = IncCounter(@"D:\1.txt", FileType.TypeA);
    
            Console.WriteLine($"TypeA : {result.FileTypeACounter} - TypeB : {result.FileTypeBCounter}");
    
            Console.Read();
        }
    
        public enum FileType
        {
            TypeA,
            TypeB
        }
    
        [Serializable]
        public class FileTypeCounter
        {
            public int FileTypeACounter { get; set; }
            public int FileTypeBCounter { get; set; }
        }
    
        public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false)
        {
            using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
            {
                var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                binaryFormatter.Serialize(stream, objectToWrite);
            }
        }
    
        public static T ReadFromBinaryFile<T>(string filePath)
        {
            using (Stream stream = File.Open(filePath, FileMode.Open))
            {
                var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                return (T)binaryFormatter.Deserialize(stream);
            }
        }
    
        public static FileTypeCounter IncCounter(string counterFilePath, FileType fileType)
        {
            try
            {
                var fileTypeCounter = new FileTypeCounter { FileTypeACounter = 0, FileTypeBCounter = 0 };
    
                if (!File.Exists(counterFilePath))
                {
                    if (fileType == FileType.TypeA)
                        fileTypeCounter.FileTypeACounter++;
                    else
                        fileTypeCounter.FileTypeBCounter++;
    
                    WriteToBinaryFile<FileTypeCounter>(counterFilePath, fileTypeCounter);
                    return fileTypeCounter;
                }
    
                fileTypeCounter = ReadFromBinaryFile<FileTypeCounter>(counterFilePath);
    
                switch (fileType)
                {
                    case FileType.TypeA:
                        fileTypeCounter.FileTypeACounter++;
                        break;
    
                    case FileType.TypeB:
                        fileTypeCounter.FileTypeBCounter++;
                        break;
                }
    
                WriteToBinaryFile<FileTypeCounter>(counterFilePath, fileTypeCounter);
    
                return fileTypeCounter;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    
        2
  •  1
  •   TheGeneral    6 年前

    要么

    • 将其存储在内存中,并在更改时将数据保存到文件或注册表中。
    • 或者将其存储在数据库中,或者创建一个包含任务和开始日期的表。你可以用各种方式查询它

    当服务加载读取该数据时,将其存储在内存中。

    作业完成