0

I can't figure out how to call last part of this method:

private static async Task ForEachFileAsync(string path, string searchPattern, SearchOption searchOption, Func<string, Task> doAsync)
{
    // Avoid blocking the caller for the initial enumerate call.
    await Task.Yield();

    foreach (string file in Directory.EnumerateFiles(path, searchPattern, searchOption))
    {
        await doAsync(file);
    }
}

This is what i have managed to do so far:

private void Stats_Load(object sender, EventArgs e)
{
     ForEachFileAsync(@"E:\Path", "*", SearchOption.TopDirectoryOnly, what to put here? );
}

I Did not need an explanation of how System.Func works!! I only needed to know how to make a method call using System.Func which WAS NOT found at "Can someone explain what the C# "Func<T,T>" does?".

Adriani6 answer was the best and most helpful. Thanks

MrJack Mcfreder
  • 692
  • 1
  • 10
  • 21
  • No! That question explains func T and there isn't a single example of method call usage since that's what I needed at the first place. – MrJack Mcfreder Aug 29 '18 at 14:47

2 Answers2

2

Short answer: async (file) => /*perform async operation*/


As described on Microsoft Docs, a Func (Function) is used for specifying a method.

It's very similar to the way Action works, exception Actions returns Void, whereas Functions returns the last generic parameter. For example:

Func<int, int, string> Would take the following method as an argument:

string AddNumbers(int value1, int value2)
{
    return (value1 + value2).ToString()
}

In the case of a Task, due to syntactic sugar implemented along with Tasks, you can perform the Await operations in a method marked as Async. For example:

Func<string, Task> Would take the following method as an argument:

async Task AddNumbers(string url)
{
    var client = new HttpClient();
    await client.PostAsync(url);
}

Lamda expressions are shorthands for inline methods. So async (url) => await new HttpClient().PostAsync(url); is the same as the method written above.

In this case you'll probably just want to execute some asynchronous read/write operation on your hard drive.

Falgantil
  • 1,290
  • 12
  • 27
1

You want to pass a delegate to that method which accepts a string and returns a Task.. taken from the signature System.Func<in T, out TResult>

private static Task DoSomething(string task)
{
    throw new NotImplementedException();
}

And you would put that reference to that method..

ForEachFileAsync(@"E:\Path", "*", SearchOption.TopDirectoryOnly, (str) => DoSomething(str));

You obviously don't want throw new NotImplementedException(); in the body of DoSomething... you want to implement your own method.

Adrian
  • 8,271
  • 2
  • 26
  • 43
  • Thank you the example and explanation ! – MrJack Mcfreder Aug 29 '18 at 11:26
  • I'd appreciate some feedback for the down-vote rather than just leaving a negative and keeping feedback to yourself, that doesn't help anyone. – Adrian Aug 29 '18 at 13:22
  • 1
    That wasn't me! But you're right.. I am sure the same guy that down voted your answer also down voted my question lol! Your answer was the most helpful so thanks again! – MrJack Mcfreder Aug 29 '18 at 14:44