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) {
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.Name = "ImagePrint";
thread.Start();
Thread thread;
Server.StartThread(out thread, "ImagePrint",
() => DoDrawImage(p, m, (DrawArgs)state));
return false;
}

View File

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

View File

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

View File

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

View File

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

View File

@ -335,6 +335,14 @@ namespace MCGalaxy
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
static bool AcceptableSaltChar(char c) {

View File

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

View File

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