Created
November 8, 2017 13:54
-
-
Save SecretDeveloper/0ce4200ec30f5981b2f7006c40a7148f to your computer and use it in GitHub Desktop.
TimedDisposed
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
public class TimedDisposed:IDisposable | |
{ | |
Action<TimeSpan> _action; | |
Action<TimeSpan, string> _markAction; | |
DateTime _start; | |
List<KeyValuePair<DateTime, string>> _marks = new List<KeyValuePair<DateTime, string>>(); | |
public bool ActionEnabled { get; set; } | |
public bool MarkActionEnabled { get; set; } | |
public TimedDisposed(Action<TimeSpan> action, Action<TimeSpan, string> markAction = null) | |
{ | |
ActionEnabled = true; | |
MarkActionEnabled = true; | |
_action = action; | |
_markAction = markAction; | |
_start = DateTime.UtcNow; | |
} | |
public void Mark(string message) | |
{ | |
_marks.Add(new KeyValuePair<DateTime, string>(DateTime.UtcNow, message)); | |
} | |
public void Dispose() | |
{ | |
try | |
{ | |
if(ActionEnabled && _action != null) | |
{ | |
var stop = DateTime.UtcNow; | |
_action(stop-_start); | |
if(MarkActionEnabled && _markAction != null) | |
{ | |
_marks.ForEach(mark => _markAction(mark.Key - _start, mark.Value)); | |
} | |
} | |
} | |
catch{} | |
Dispose(true); | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
try | |
{ | |
_action = null; | |
_markAction = null; | |
} | |
catch{} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment