Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13582

Pass a method as a parameter to another method

I am having a method called LoadData which gets data from DataBase and fills a DataGridView.

I am using a Stopwatch to measure how long my method takes to finish it's job as 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 something that can do The following :

private void MeasureTime(Method m)
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();
    m.Invoke();
    sw.Stop();
    ShowTakenTime(sw.ElapsedMilliseconds);
}

so that I can pass the LoadData method to it and it does the rest for me.

MeasureTime(LoadData());

How can I do that?

Upvotes: 6

Views: 1628

Answers (5)

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

define a delegate in your class with any signature you want:

delegate int YourDelegate(string s);

a method that get delegate and run it

void CallerMethod(YourDelegate method)
{
    int result = method("");
}

method is going to pass and is compatible with YourDelegate:

int MethodToPass(string s)
{
    throw new exception();
}

pass MethodToPass to CallerMewthod:

CallerMethod(MethodToPass);

also you can instead of declaring a new delegates, you can use predefined Func<output, inputType1, inputType2, ...> generic delegate that supports any type of input and output

Upvotes: 0

ElGauchooo
ElGauchooo

Reputation: 4646

If you want to pass parameters to your Method m, use an Action<...>, for example if you want to pass an int and a string to your Method, use Action<int,string>

private void MeasureTime(Action<int, string> m)
{
   var sw = new System.Diagnostics.Stopwatch();
   sw.Start();
   m(42, "Hello World");
   sw.Stop();
   ShowTakenTime(sw.ElapsedMilliseconds);
}

If you need to access the return value of your Method, use Func<...>:

private void MeasureTime(Func<int, string, string> m)
{
   var sw = new System.Diagnostics.Stopwatch();
   sw.Start();
   var result = m(42, "Hello World");
   sw.Stop();
   ShowTakenTime(sw.ElapsedMilliseconds);
}

Upvotes: 1

Mateusz
Mateusz

Reputation: 2317

You can use Action because you are not returning anything:

http://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx

 MeasureTime(Action action)
 Action act = LoadData;
 act.Invoke();

Upvotes: 0

mgukov
mgukov

Reputation: 493

Use delegates

private void MeasureTime(System.Action a)
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();
    a();
    sw.Stop();
    ShowTakenTime(sw.ElapsedMilliseconds);
}

Upvotes: 0

AlexH
AlexH

Reputation: 2700

For a method without parameters and returning void, you can use Action:

    private void MeasureTime(Action m)
    {
        var sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        m();
        sw.Stop();
        ShowTakenTime(sw.ElapsedMilliseconds);
    }

If you have some parameters or a return type, use Func

Upvotes: 3

Related Questions