From 4c08ad0064c89b86990fc1ce278bb856e44e6371 Mon Sep 17 00:00:00 2001 From: Timerix Date: Sun, 22 Sep 2024 23:02:23 +0500 Subject: [PATCH] improvements for File and Timer --- DTLib/DTLib.csproj | 2 +- DTLib/DTLib.csproj.DotSettings | 3 +- DTLib/Filesystem/File.cs | 7 +++-- DTLib/Filesystem/Path.cs | 11 ++++++- DTLib/Logging/Loggers/FileLogger.cs | 6 ++-- DTLib/Timer.cs | 47 ++++++++++++++++++----------- 6 files changed, 50 insertions(+), 26 deletions(-) diff --git a/DTLib/DTLib.csproj b/DTLib/DTLib.csproj index 4c05b31..267339f 100644 --- a/DTLib/DTLib.csproj +++ b/DTLib/DTLib.csproj @@ -2,7 +2,7 @@ DTLib - 1.4.0 + 1.4.1 Timerix Library for all my C# projects GIT diff --git a/DTLib/DTLib.csproj.DotSettings b/DTLib/DTLib.csproj.DotSettings index d72a4eb..d0157c7 100644 --- a/DTLib/DTLib.csproj.DotSettings +++ b/DTLib/DTLib.csproj.DotSettings @@ -1,2 +1,3 @@  - False \ No newline at end of file + False + True \ No newline at end of file diff --git a/DTLib/Filesystem/File.cs b/DTLib/Filesystem/File.cs index 80bd961..e6bc4eb 100644 --- a/DTLib/Filesystem/File.cs +++ b/DTLib/Filesystem/File.cs @@ -83,7 +83,8 @@ public static class File { if (!Exists(file)) throw new Exception($"file not found: <{file}>"); - return System.IO.File.Open(file.Str, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); + return System.IO.File.Open(file.Str, System.IO.FileMode.Open, System.IO.FileAccess.Read, + System.IO.FileShare.ReadWrite | System.IO.FileShare.Delete); } public static System.IO.FileStream OpenWrite(IOPath file) @@ -91,13 +92,13 @@ public static class File if (Exists(file)) Delete(file); Create(file); - return System.IO.File.Open(file.Str, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); + return System.IO.File.Open(file.Str, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Read); } public static System.IO.FileStream OpenAppend(IOPath file) { Create(file); - return System.IO.File.Open(file.Str, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); + return System.IO.File.Open(file.Str, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read); } public static void CreateSymlink(IOPath sourcePath, IOPath symlinkPath) diff --git a/DTLib/Filesystem/Path.cs b/DTLib/Filesystem/Path.cs index f556dbf..a483db3 100644 --- a/DTLib/Filesystem/Path.cs +++ b/DTLib/Filesystem/Path.cs @@ -99,10 +99,19 @@ public static class Path public static IOPath Extension(this IOPath path) { int i = path.LastIndexOf('.'); - if (i == -1) return LastName(path); + if (i == -1) + return LastName(path); return path.Substring(i + 1); } + public static IOPath RemoveExtension(this IOPath path) + { + int i = path.LastIndexOf('.'); + if (i > 0) + return path.Substring(0, i); + return path; + } + public static IOPath ParentDir(this IOPath path) { int i = path.LastIndexOf(Sep); diff --git a/DTLib/Logging/Loggers/FileLogger.cs b/DTLib/Logging/Loggers/FileLogger.cs index e4c9b82..a34f3ad 100644 --- a/DTLib/Logging/Loggers/FileLogger.cs +++ b/DTLib/Logging/Loggers/FileLogger.cs @@ -15,7 +15,7 @@ public class FileLogger : ILogger { Format = format; LogfileName = logfile; - LogfileStream = File.OpenAppend(logfile); + LogfileStream = File.OpenWrite(logfile); } public FileLogger(IOPath logfile) : this(logfile, new DefaultLogFormat()) @@ -46,8 +46,8 @@ public class FileLogger : ILogger { try { - LogfileStream?.Flush(); - LogfileStream?.Dispose(); + LogfileStream.Flush(); + LogfileStream.Dispose(); } catch (ObjectDisposedException) { } } diff --git a/DTLib/Timer.cs b/DTLib/Timer.cs index 3200d8a..4fd0fc5 100644 --- a/DTLib/Timer.cs +++ b/DTLib/Timer.cs @@ -1,13 +1,11 @@ -using System.Threading; - -namespace DTLib; +namespace DTLib; // // простой и понятный класс для выполнения каких-либо действий в отдельном потоке раз в некоторое время // -public class Timer +public class Timer : IDisposable { - private Task _timerTask; + private Task? _timerTask; private CancellationTokenSource _cts = new(); private bool _repeat; private int _delayMs; @@ -19,27 +17,42 @@ public class Timer _repeat = repeat; _delayMs = delayMs; _action = action; - _timerTask = new Task(Loop, _cts.Token); } - private void Loop() + public void InvokeAction() { - do - { - if (_cts.Token.IsCancellationRequested) - return; - Task.Delay(_delayMs).Wait(); - if (_cts.Token.IsCancellationRequested) - return; - _action?.Invoke(); - } while (_repeat); + _action(); } - public void Start() => _timerTask.Start(); + public void Start() + { + _timerTask = new Task(Loop); + _timerTask.Start(); + } // завершение потока public void Stop() { _cts.Cancel(); + _cts = new CancellationTokenSource(); + } + + public void Dispose() + { + Stop(); + } + + private void Loop() + { + var ct = _cts.Token; + do + { + if (ct.IsCancellationRequested) + return; + Task.Delay(_delayMs).Wait(); + if (ct.IsCancellationRequested) + return; + InvokeAction(); + } while (_repeat); } }