.NET 5+ Support (#1674)

Implement changes to support .NET 5 onwards.
Co-authored-by: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com>
Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
breadbyte 2022-07-03 22:34:07 +08:00 committed by GitHub
parent b3cc2351ee
commit d9f1a77ac2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
117 changed files with 1028 additions and 9058 deletions

View file

@ -11,8 +11,8 @@ namespace MinecraftClient
/// </summary>
public class FileMonitor : IDisposable
{
private FileSystemWatcher monitor = null;
private Thread polling = null;
private Tuple<FileSystemWatcher, CancellationTokenSource>? monitor = null;
private Tuple<Thread, CancellationTokenSource>? polling = null;
/// <summary>
/// Create a new FileMonitor and start monitoring
@ -30,13 +30,13 @@ namespace MinecraftClient
try
{
monitor = new FileSystemWatcher();
monitor.Path = folder;
monitor.IncludeSubdirectories = false;
monitor.Filter = filename;
monitor.NotifyFilter = NotifyFilters.LastWrite;
monitor.Changed += handler;
monitor.EnableRaisingEvents = true;
monitor = new Tuple<FileSystemWatcher, CancellationTokenSource>(new FileSystemWatcher(), new CancellationTokenSource());
monitor.Item1.Path = folder;
monitor.Item1.IncludeSubdirectories = false;
monitor.Item1.Filter = filename;
monitor.Item1.NotifyFilter = NotifyFilters.LastWrite;
monitor.Item1.Changed += handler;
monitor.Item1.EnableRaisingEvents = true;
}
catch
{
@ -47,9 +47,10 @@ namespace MinecraftClient
}
monitor = null;
polling = new Thread(() => PollingThread(folder, filename, handler));
polling.Name = String.Format("{0} Polling thread: {1}", this.GetType().Name, Path.Combine(folder, filename));
polling.Start();
var cancellationTokenSource = new CancellationTokenSource();
polling = new Tuple<Thread, CancellationTokenSource>(new Thread(() => PollingThread(folder, filename, handler, cancellationTokenSource.Token)), cancellationTokenSource);
polling.Item1.Name = String.Format("{0} Polling thread: {1}", this.GetType().Name, Path.Combine(folder, filename));
polling.Item1.Start();
}
}
@ -59,9 +60,9 @@ namespace MinecraftClient
public void Dispose()
{
if (monitor != null)
monitor.Dispose();
monitor.Item1.Dispose();
if (polling != null)
polling.Abort();
polling.Item2.Cancel();
}
/// <summary>
@ -70,11 +71,11 @@ namespace MinecraftClient
/// <param name="folder">Folder to monitor</param>
/// <param name="filename">File name to monitor</param>
/// <param name="handler">Callback when file changes</param>
private void PollingThread(string folder, string filename, FileSystemEventHandler handler)
private void PollingThread(string folder, string filename, FileSystemEventHandler handler, CancellationToken cancellationToken)
{
string filePath = Path.Combine(folder, filename);
DateTime lastWrite = GetLastWrite(filePath);
while (true)
while (!cancellationToken.IsCancellationRequested)
{
Thread.Sleep(5000);
DateTime lastWriteNew = GetLastWrite(filePath);