Created
August 9, 2014 23:46
-
-
Save XoseLluis/acf992cf084911721a94 to your computer and use it in GitHub Desktop.
Sample of how to Suspend, Resume, Abort Threads in .Net
This file contains 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
// deploytonenyures.blogspot.com | |
using System; | |
using System.IO; | |
using System.Threading; | |
enum Status | |
{ | |
NotStarted, | |
Running, | |
Suspended | |
} | |
class ActionPerformer | |
{ | |
//use this WaitHandle to signal suspend(Reset)/resume(Set) requests | |
private EventWaitHandle resumeSuspend; | |
private bool abort; | |
private Status status; | |
public ActionPerformer() | |
{ | |
//create it in Set state, so it starts to run as we call Run | |
this.resumeSuspend = new AutoResetEvent(true); | |
this.status = Status.NotStarted; | |
this.abort = false; | |
} | |
public void Run() | |
{ | |
if (this.status == Status.NotStarted) | |
{ | |
this.status = Status.Running; | |
new Thread(() => { | |
while(!this.abort){ //check for Abort requests | |
Console.WriteLine("Performing Action"); | |
Thread.Sleep(500); | |
this.resumeSuspend.WaitOne(); //check for Resume/Suspend requests | |
} | |
}).Start(); | |
} | |
} | |
public void Suspend() | |
{ | |
if (this.status == Status.Running) | |
{ | |
this.status = Status.Suspended; | |
this.resumeSuspend.Reset(); | |
Console.WriteLine("Being Suspended"); | |
} | |
else | |
{ | |
Console.WriteLine("Can't be suspended if not running"); | |
} | |
} | |
public void Resume() | |
{ | |
if (this.status == Status.Suspended) | |
{ | |
this.status = Status.Running; | |
this.resumeSuspend.Set(); | |
Console.WriteLine("Being Resumed"); | |
} | |
else | |
{ | |
Console.WriteLine("Can't be resumed if not suspended"); | |
} | |
} | |
public void Abort() | |
{ | |
Console.WriteLine("Being Aborted"); | |
this.abort = true; | |
if (this.status == Status.Suspended){ | |
//if it's suspended we have to resume it so the loop cheks for abort and stops | |
this.resumeSuspend.Set(); | |
} | |
} | |
} |
This file contains 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
// deploytonenyures.blogspot.com | |
// csc App.cs ActionPerformer.cs | |
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Threading; | |
using System.Web.Script.Serialization; | |
class DoCopy | |
{ | |
public static void Main(String[] args) | |
{ | |
Console.WriteLine("Program started"); | |
ActionPerformer actionPerformer = new ActionPerformer(); | |
string option = ""; | |
Console.WriteLine("S to Suspend, R to Resume, A to Abort"); | |
actionPerformer.Run(); | |
while (option != "a") | |
{ | |
option = Console.ReadLine().ToLower(); | |
switch (option) | |
{ | |
case "a": | |
actionPerformer.Abort(); | |
break; | |
case "s": | |
actionPerformer.Suspend(); | |
break; | |
case "r": | |
actionPerformer.Resume(); | |
break; | |
} | |
} | |
Console.WriteLine("Press key to exit"); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment