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

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -