using System.Threading;

namespace QCRPG.Framework;

public abstract class WorkerThreadBase
{
	protected Logger logger;

	private Thread thread;

	protected ManualResetEvent shutdownThreadEvent = new ManualResetEvent(initialState: false);

	private bool running;

	public WorkerThreadBase(Logger logger)
	{
		this.logger = logger;
	}

	public void Startup()
	{
		thread = new Thread(Run);
		thread.IsBackground = true;
		thread.Name = "WorkerThreadBase";
		thread.Start();
		running = true;
	}

	public void Shutdown()
	{
		if (running)
		{
			shutdownThreadEvent.Set();
			thread.Join();
			shutdownThreadEvent.Reset();
			running = false;
		}
	}

	protected virtual void Run()
	{
		shutdownThreadEvent.WaitOne();
	}
}
