c# - Pass a method as a parameter to another method -
i having method called loaddata gets data database , fills datagridview
.
i using stopwatch measure how long method takes finish it's job below :
private void btnloaddata_click(object sender, eventargs e) { var sw = new system.diagnostics.stopwatch(); sw.start(); loaddata (); sw.stop(); showtakentime(sw.elapsedmilliseconds); }
i want can following :
private void measuretime(method m) { var sw = new system.diagnostics.stopwatch(); sw.start(); m.invoke(); sw.stop(); showtakentime(sw.elapsedmilliseconds); }
so can pass loaddata method , rest me.
measuretime(loaddata());
how can that?
for method without parameters , returning void, can use action:
private void measuretime(action m) { var sw = new system.diagnostics.stopwatch(); sw.start(); m(); sw.stop(); showtakentime(sw.elapsedmilliseconds); }
if have parameters or return type, use func
Comments
Post a Comment