Skip to content

Instantly share code, notes, and snippets.

@FoxCouncil
Created August 23, 2020 22:24
Show Gist options
  • Save FoxCouncil/3accf357fbf18f5b97a2677aaea54f0d to your computer and use it in GitHub Desktop.
Save FoxCouncil/3accf357fbf18f5b97a2677aaea54f0d to your computer and use it in GitHub Desktop.
A threaded Manager class...
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