0

I want to pass in a void or an int/string/bool(Which returns a value) Dynamically like so.

Delay(MyVoid);//I wont to execute a delay here, after the delay it will execute the the param/void like so...
public static void MyVoid()
{
    MessageBox.Show("The void has started!");
}
public async Task MyAsyncMethod(void V)
{
    await Task.Delay(2000);
    V()
}

ps, I have tried using Delegates but it doesn't let be use it as a parameter.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Daniel Jones
  • 109
  • 1
  • 2
  • 9
  • 1
    In the future, instead of re-asking a question, *please* edit your original question. If I had seen that question before posting my answer I would've simply voted to close this one. As it stands now, I recommend you delete your other question. – p.s.w.g Jul 18 '13 at 04:17

1 Answers1

4

Use an Action delegate to execute a method which returns void:

public async Task MyAsyncMethod(Action V)
{
    await Task.Delay(2000);
    V();
}

Or Func<T> for a method which returns some value

public async Task MyAsyncMethod(Func<int> V)
{
    await Task.Delay(2000);
    int result = V();
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • One other question, how do i get the public async task to return an intager? – Daniel Jones Jul 18 '13 at 04:33
  • @DanielJones [How to: Return a Value from a Task](http://msdn.microsoft.com/en-us/library/dd537613.aspx) – p.s.w.g Jul 18 '13 at 04:35
  • I need Task task3 = Task.Factory.StartNew(() => to work as an async – Daniel Jones Jul 18 '13 at 04:41
  • @DanielJones The link I provided includes a very good, clear description of how to do exactly that. Try it out, see if you can figure it out on your own, and if you're still having trouble you can ask that as a new question here on SO. – p.s.w.g Jul 18 '13 at 05:04
  • Don't worry i have already figured it out for my self thank you for helping! – Daniel Jones Jul 18 '13 at 05:04