Created
August 23, 2020 22:24
-
-
Save FoxCouncil/3accf357fbf18f5b97a2677aaea54f0d to your computer and use it in GitHub Desktop.
A threaded Manager class...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Manager | |
{ | |
private readonly AutoResetEvent _reset = new AutoResetEvent(false); | |
private Thread _mainThread; | |
public bool IsRunning => _mainThread != null && _mainThread.IsAlive; | |
public Manager() | |
{ | |
Start(); | |
} | |
public void Start() | |
{ | |
if (IsRunning) | |
{ | |
return; | |
} | |
_mainThread = new Thread(Run); | |
// Do your Setup | |
_mainThread.Start(); | |
} | |
public void Stop() | |
{ | |
_reset.Set(); | |
// Undo your Setup | |
} | |
private void Run() | |
{ | |
Log.WriteLine($"Manager Started, Thread {_mainThread.ManagedThreadId}"); | |
_reset.WaitOne(); | |
Log.WriteLine($"Manager Quitting Thread {_mainThread.ManagedThreadId}"); | |
_mainThread = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment