Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Threading;
- using UnityEngine;
- namespace Swordfish {
- namespace threading
- {
- public class ThreadWorker
- {
- private volatile bool stop = false;
- private volatile bool pause = false;
- private Thread thread = null;
- public ThreadWorker()
- {
- thread = new Thread(Tick);
- }
- public void Start()
- {
- stop = false;
- pause = false;
- thread.Start();
- }
- public void Stop()
- {
- stop = true;
- }
- public void Restart()
- {
- stop = false;
- pause = false;
- thread.Abort();
- thread.Start();
- }
- public void Pause()
- {
- pause = true;
- }
- public void Unpause()
- {
- pause = false;
- }
- public void TogglePause()
- {
- pause = !pause;
- }
- public void Kill()
- {
- thread.Abort();
- }
- private void Tick()
- {
- while (stop == false)
- {
- while (pause == false)
- {
- Run();
- }
- Thread.Sleep(200); // Sleep when paused
- }
- // Stopped thread safely
- }
- public virtual void Run() {}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement