2023-01-28 20:51:29 +08:00
|
|
|
|
using RageCoop.Client.Scripting;
|
|
|
|
|
using RageCoop.Core;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace RageCoop.Client
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Needed to properly stop all thread when the module unloads
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static class ThreadManager
|
|
|
|
|
{
|
2023-03-06 21:54:41 +08:00
|
|
|
|
private static readonly List<Thread> _threads = new();
|
|
|
|
|
private static readonly Thread _watcher = new(RemoveStopped);
|
|
|
|
|
static ThreadManager()
|
|
|
|
|
{
|
|
|
|
|
_watcher.Start();
|
|
|
|
|
}
|
|
|
|
|
private static void RemoveStopped()
|
2023-01-28 20:51:29 +08:00
|
|
|
|
{
|
2023-02-13 20:44:50 +08:00
|
|
|
|
while (!IsUnloading)
|
2023-01-28 20:51:29 +08:00
|
|
|
|
{
|
|
|
|
|
lock (_threads)
|
|
|
|
|
{
|
|
|
|
|
_threads.RemoveAll(t => !t.IsAlive);
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static Thread CreateThread(Action callback, string name = "CoopThread", bool startNow = true)
|
|
|
|
|
{
|
|
|
|
|
lock (_threads)
|
|
|
|
|
{
|
|
|
|
|
var created = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
catch (ThreadInterruptedException) { }
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-02-13 17:51:18 +08:00
|
|
|
|
Log.Error($"Unhandled exception caught in thread {Environment.CurrentManagedThreadId}", ex);
|
2023-01-28 20:51:29 +08:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2023-02-13 17:51:18 +08:00
|
|
|
|
Log.Debug($"Thread stopped: " + Environment.CurrentManagedThreadId);
|
2023-01-28 20:51:29 +08:00
|
|
|
|
}
|
2023-03-06 21:54:41 +08:00
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
Name = name
|
|
|
|
|
};
|
2023-02-13 17:51:18 +08:00
|
|
|
|
Log.Debug($"Thread created: {name}, id: {created.ManagedThreadId}");
|
2023-01-28 20:51:29 +08:00
|
|
|
|
_threads.Add(created);
|
|
|
|
|
if (startNow) created.Start();
|
|
|
|
|
return created;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void OnUnload()
|
|
|
|
|
{
|
2023-02-13 20:44:50 +08:00
|
|
|
|
Log.Debug("Stopping background threads");
|
2023-01-28 20:51:29 +08:00
|
|
|
|
lock (_threads)
|
|
|
|
|
{
|
|
|
|
|
foreach (var thread in _threads)
|
|
|
|
|
{
|
|
|
|
|
if (thread.IsAlive)
|
|
|
|
|
{
|
2023-02-13 17:51:18 +08:00
|
|
|
|
Log.Debug($"Waiting for thread {thread.ManagedThreadId} to stop");
|
2023-01-28 20:51:29 +08:00
|
|
|
|
// thread.Interrupt(); PlatformNotSupportedException ?
|
|
|
|
|
thread.Join();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_threads.Clear();
|
|
|
|
|
}
|
2023-02-13 20:44:50 +08:00
|
|
|
|
Log.Debug("Stopping thread watcher");
|
|
|
|
|
_watcher.Join();
|
2023-01-28 20:51:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|