Fix rarely throwing error due to trying to set IsBackground on a dead thread

This commit is contained in:
UnknownShadow200 2024-10-10 17:37:09 +11:00
parent 3401665c67
commit 9cb2cf1637
8 changed files with 15 additions and 8 deletions

View File

@ -107,7 +107,7 @@ namespace MCGalaxy.Commands.Building {
Thread thread;
Server.StartThread(out thread, "ImagePrint",
() => DoDrawImage(p, m, (DrawArgs)state));
thread.IsBackground = true;
Utils.SetBackgroundMode(thread);
return false;
}

View File

@ -84,7 +84,7 @@ namespace MCGalaxy.Games
Thread thread;
Server.StartThread(out thread, "Game_ " + GameName, RunGame);
thread.IsBackground = true;
Utils.SetBackgroundMode(thread);
}
/// <summary> Attempts to auto start this game with infinite rounds. </summary>

View File

@ -58,7 +58,7 @@ namespace MCGalaxy {
Server.StartThread(out physThread, "Physics_" + name,
PhysicsLoop);
physThread.IsBackground = true;
Utils.SetBackgroundMode(physThread);
physThreadStarted = true;
}
}

View File

@ -203,7 +203,7 @@ namespace MCGalaxy.Modules.Relay
void RunAsync() {
Server.StartThread(out worker, RelayName + "_RelayBot",
IOThread);
worker.IsBackground = true;
Utils.SetBackgroundMode(worker);
}
protected abstract void DoConnect();

View File

@ -558,7 +558,7 @@ namespace MCGalaxy
Thread thread;
Server.StartThread(out thread, "CMD_ " + cmd, callback);
thread.IsBackground = true;
Utils.SetBackgroundMode(thread);
} catch (Exception e) {
Logger.LogError(e);
Message("&WCommand failed");
@ -585,7 +585,7 @@ namespace MCGalaxy
Thread thread;
Server.StartThread(out thread, "CMDS_",
() => UseCommands(commands, messages, data));
thread.IsBackground = true;
Utils.SetBackgroundMode(thread);
} catch (Exception e) {
Logger.LogError(e);
Message("&WCommand failed.");

View File

@ -68,7 +68,7 @@ namespace MCGalaxy
public void RunAsync() {
Thread worker;
Server.StartThread(out worker, ThreadName, SendLoop);
worker.IsBackground = true;
Utils.SetBackgroundMode(worker);
}
public void StopAsync() {

View File

@ -83,7 +83,7 @@ namespace MCGalaxy.UI
Logger.Log(LogType.CommandUsage, "(console): FAILED COMMAND");
}
});
thread.IsBackground = true;
Utils.SetBackgroundMode(thread);
}
public static string Format(string message) {

View File

@ -20,6 +20,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
namespace MCGalaxy
{
@ -92,5 +93,11 @@ namespace MCGalaxy
static char HexEncode(int i) {
return i < 10 ? (char)(i + '0') : (char)((i - 10) + 'a');
}
public static void SetBackgroundMode(Thread thread) {
// Throws an exception when called on a dead thread,
// which can very rarely happen
try { thread.IsBackground = true; } catch { }
}
}
}