Created
May 10, 2019 04:46
-
-
Save adammendoza/2683cb76d9268f9d18fc4ad6349a673c to your computer and use it in GitHub Desktop.
C# PauseClass
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 PauseClass | |
{ | |
//(C) Michael Roberg | |
//Please feel free to distribute this class but include my credentials. | |
// usage: var p = new PauseClass(); p.Pause(1000); // pause for 1 second | |
System.Timers.Timer pauseTimer = null; | |
public void BreakPause() | |
{ | |
if (pauseTimer != null) | |
{ | |
pauseTimer.Stop(); | |
pauseTimer.Enabled = false; | |
} | |
} | |
public bool Pause(int miliseconds) | |
{ | |
ThreadPriority CurrentPriority = Thread.CurrentThread.Priority; | |
if (miliseconds > 0) | |
{ | |
Thread.CurrentThread.Priority = ThreadPriority.Lowest; | |
pauseTimer = new System.Timers.Timer(); | |
pauseTimer.Elapsed += new ElapsedEventHandler(pauseTimer_Elapsed); | |
pauseTimer.Interval = miliseconds; | |
pauseTimer.Enabled = true; | |
while (pauseTimer.Enabled) | |
{ | |
Thread.Sleep(10); | |
Application.DoEvents(); | |
//pausThread.Sleep(1); | |
} | |
pauseTimer.Elapsed -= new ElapsedEventHandler(pauseTimer_Elapsed); | |
} | |
Thread.CurrentThread.Priority = CurrentPriority; | |
return true; | |
} | |
private void pauseTimer_Elapsed(object sender, ElapsedEventArgs e) | |
{ | |
pauseTimer.Enabled = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment