improvements for File and Timer
This commit is contained in:
parent
2c790f9b29
commit
4c08ad0064
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib</PackageId>
|
<PackageId>DTLib</PackageId>
|
||||||
<Version>1.4.0</Version>
|
<Version>1.4.1</Version>
|
||||||
<Authors>Timerix</Authors>
|
<Authors>Timerix</Authors>
|
||||||
<Description>Library for all my C# projects</Description>
|
<Description>Library for all my C# projects</Description>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
|
|||||||
@ -1,2 +1,3 @@
|
|||||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=console/@EntryIndexedValue">False</s:Boolean></wpf:ResourceDictionary>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=console/@EntryIndexedValue">False</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=logging_005Cloggers/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||||
@ -83,7 +83,8 @@ public static class File
|
|||||||
{
|
{
|
||||||
if (!Exists(file))
|
if (!Exists(file))
|
||||||
throw new Exception($"file not found: <{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)
|
public static System.IO.FileStream OpenWrite(IOPath file)
|
||||||
@ -91,13 +92,13 @@ public static class File
|
|||||||
if (Exists(file))
|
if (Exists(file))
|
||||||
Delete(file);
|
Delete(file);
|
||||||
Create(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)
|
public static System.IO.FileStream OpenAppend(IOPath file)
|
||||||
{
|
{
|
||||||
|
|
||||||
Create(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)
|
public static void CreateSymlink(IOPath sourcePath, IOPath symlinkPath)
|
||||||
|
|||||||
@ -99,10 +99,19 @@ public static class Path
|
|||||||
public static IOPath Extension(this IOPath path)
|
public static IOPath Extension(this IOPath path)
|
||||||
{
|
{
|
||||||
int i = path.LastIndexOf('.');
|
int i = path.LastIndexOf('.');
|
||||||
if (i == -1) return LastName(path);
|
if (i == -1)
|
||||||
|
return LastName(path);
|
||||||
return path.Substring(i + 1);
|
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)
|
public static IOPath ParentDir(this IOPath path)
|
||||||
{
|
{
|
||||||
int i = path.LastIndexOf(Sep);
|
int i = path.LastIndexOf(Sep);
|
||||||
|
|||||||
@ -15,7 +15,7 @@ public class FileLogger : ILogger
|
|||||||
{
|
{
|
||||||
Format = format;
|
Format = format;
|
||||||
LogfileName = logfile;
|
LogfileName = logfile;
|
||||||
LogfileStream = File.OpenAppend(logfile);
|
LogfileStream = File.OpenWrite(logfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileLogger(IOPath logfile) : this(logfile, new DefaultLogFormat())
|
public FileLogger(IOPath logfile) : this(logfile, new DefaultLogFormat())
|
||||||
@ -46,8 +46,8 @@ public class FileLogger : ILogger
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
LogfileStream?.Flush();
|
LogfileStream.Flush();
|
||||||
LogfileStream?.Dispose();
|
LogfileStream.Dispose();
|
||||||
}
|
}
|
||||||
catch (ObjectDisposedException) { }
|
catch (ObjectDisposedException) { }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 CancellationTokenSource _cts = new();
|
||||||
private bool _repeat;
|
private bool _repeat;
|
||||||
private int _delayMs;
|
private int _delayMs;
|
||||||
@ -19,27 +17,42 @@ public class Timer
|
|||||||
_repeat = repeat;
|
_repeat = repeat;
|
||||||
_delayMs = delayMs;
|
_delayMs = delayMs;
|
||||||
_action = action;
|
_action = action;
|
||||||
_timerTask = new Task(Loop, _cts.Token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Loop()
|
public void InvokeAction()
|
||||||
{
|
{
|
||||||
do
|
_action();
|
||||||
{
|
|
||||||
if (_cts.Token.IsCancellationRequested)
|
|
||||||
return;
|
|
||||||
Task.Delay(_delayMs).Wait();
|
|
||||||
if (_cts.Token.IsCancellationRequested)
|
|
||||||
return;
|
|
||||||
_action?.Invoke();
|
|
||||||
} while (_repeat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start() => _timerTask.Start();
|
public void Start()
|
||||||
|
{
|
||||||
|
_timerTask = new Task(Loop);
|
||||||
|
_timerTask.Start();
|
||||||
|
}
|
||||||
|
|
||||||
// завершение потока
|
// завершение потока
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
_cts.Cancel();
|
_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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user