Created
August 8, 2018 09:26
-
-
Save chenyahui/092eb7e89dc32c45d0d895a973b4a1ab to your computer and use it in GitHub Desktop.
一个通用的C#时耗统计函数
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
class NetTimeException : Exception | |
{ | |
public string msg; | |
public long time; | |
public NetTimeException(string msg, long time) | |
{ | |
this.msg = msg; | |
this.time = time; | |
} | |
} | |
static long TimeMethodWrapper(Action action) | |
{ | |
Stopwatch stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
try | |
{ | |
action.Invoke(); | |
} | |
catch (Exception e) | |
{ | |
stopwatch.Stop(); | |
throw new NetTimeException(e.Message, stopwatch.ElapsedMilliseconds); | |
} | |
stopwatch.Stop(); | |
return stopwatch.ElapsedMilliseconds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment