Use single function for most thread creation

This commit is contained in:
UnknownShadow200 2023-12-19 22:23:39 +11:00
parent cc9a71bc1b
commit 07a1bd8925
8 changed files with 26 additions and 29 deletions

View File

@ -104,9 +104,9 @@ namespace MCGalaxy.Commands.Building {
bool DoImage(Player p, Vec3S32[] m, object state, BlockID block) { bool DoImage(Player p, Vec3S32[] m, object state, BlockID block) {
if (m[0].X == m[1].X && m[0].Z == m[1].Z) { p.Message("No direction was selected"); return false; } if (m[0].X == m[1].X && m[0].Z == m[1].Z) { p.Message("No direction was selected"); return false; }
Thread thread = new Thread(() => DoDrawImage(p, m, (DrawArgs)state)); Thread thread;
thread.Name = "ImagePrint"; Server.StartThread(out thread, "ImagePrint",
thread.Start(); () => DoDrawImage(p, m, (DrawArgs)state));
return false; return false;
} }

View File

@ -81,9 +81,8 @@ namespace MCGalaxy.Games
if (pl.level == Map) PlayerJoinedGame(pl); if (pl.level == Map) PlayerJoinedGame(pl);
} }
Thread t = new Thread(RunGame); Thread thread;
t.Name = "Game_" + GameName; Server.StartThread(out thread, "Game_ " + GameName, RunGame);
t.Start();
} }
/// <summary> Attempts to auto start this game with infinite rounds. </summary> /// <summary> Attempts to auto start this game with infinite rounds. </summary>

View File

@ -56,9 +56,8 @@ namespace MCGalaxy {
if (physThread != null && physThread.ThreadState == ThreadState.Running) return; if (physThread != null && physThread.ThreadState == ThreadState.Running) return;
if (ListCheck.Count == 0 || physThreadStarted) return; if (ListCheck.Count == 0 || physThreadStarted) return;
physThread = new Thread(PhysicsLoop); Server.StartThread(out physThread, "Physics_" + name,
physThread.Name = "Physics_" + name; PhysicsLoop);
physThread.Start();
physThreadStarted = true; physThreadStarted = true;
} }
} }

View File

@ -235,10 +235,8 @@ namespace MCGalaxy.Modules.Relay
/// <summary> Starts the read loop in a background thread </summary> /// <summary> Starts the read loop in a background thread </summary>
void RunAsync() { void RunAsync() {
worker = new Thread(IOThread); Server.StartThread(out worker, RelayName + "_RelayBot",
worker.Name = RelayName + "_RelayBot"; IOThread);
worker.IsBackground = true;
worker.Start();
} }
protected abstract void DoConnect(); protected abstract void DoConnect();

View File

@ -521,10 +521,8 @@ namespace MCGalaxy
callback = ExecuteSerialCommands; callback = ExecuteSerialCommands;
} }
Thread thread = new Thread(callback); Thread thread;
try { thread.Name = "CMD_" + cmd; } catch { } Server.StartThread(out thread, "CMD_ " + cmd, callback);
thread.IsBackground = true;
thread.Start();
} catch (Exception e) { } catch (Exception e) {
Logger.LogError(e); Logger.LogError(e);
Message("&WCommand failed"); Message("&WCommand failed");
@ -548,10 +546,9 @@ namespace MCGalaxy
messages.Add(args); commands.Add(command); messages.Add(args); commands.Add(command);
} }
Thread thread = new Thread(() => UseCommands(commands, messages, data)); Thread thread;
thread.Name = "CMDS_"; Server.StartThread(out thread, "CMDS_",
thread.IsBackground = true; () => UseCommands(commands, messages, data));
thread.Start();
} catch (Exception e) { } catch (Exception e) {
Logger.LogError(e); Logger.LogError(e);
Message("&WCommand failed."); Message("&WCommand failed.");

View File

@ -335,6 +335,14 @@ namespace MCGalaxy
sw.Elapsed.TotalMilliseconds, end / 1024.0, deltaKB); sw.Elapsed.TotalMilliseconds, end / 1024.0, deltaKB);
} }
public static void StartThread(out Thread thread, string name, ThreadStart threadFunc) {
thread = new Thread(threadFunc);
thread.IsBackground = true;
try { thread.Name = name; } catch { }
thread.Start();
}
// only want ASCII alphanumerical characters for salt // only want ASCII alphanumerical characters for salt
static bool AcceptableSaltChar(char c) { static bool AcceptableSaltChar(char c) {

View File

@ -66,10 +66,8 @@ namespace MCGalaxy
/// <summary> Starts the background worker thread </summary> /// <summary> Starts the background worker thread </summary>
public void RunAsync() { public void RunAsync() {
Thread worker = new Thread(SendLoop); Thread worker;
worker.Name = ThreadName; Server.StartThread(out worker, ThreadName, SendLoop);
worker.IsBackground = true;
worker.Start();
} }
public void StopAsync() { public void StopAsync() {

View File

@ -32,9 +32,7 @@ namespace MCGalaxy.Tasks {
volatile SchedulerTask curTask; // for .ToString() volatile SchedulerTask curTask; // for .ToString()
public Scheduler(string name) { public Scheduler(string name) {
thread = new Thread(Loop); Server.StartThread(out thread, name, Loop);
thread.Name = name;
thread.Start();
} }