Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Delegate - What is proper approach

Tags:

c#

delegates

Ok, so, I'm almost there and need just a small bit of advise to get over this hump! So, the reason i asked this questoin Async delegate - What is proper syntax? is because I'm trying to call a delegate function asynchronously (using await ...).

So, here I have a class which uses a library which accepts a delegate. The library calls the delegate when triggered. It looks something like this.

Example.Question.Base{
public static class Consumer{
    private delegate bool Redo();
    private static Redo callbackFunction;

    public static async Task<T> Go(){
        // Do some stuff ...

        // now I want to set up my delegate so this library 
        // I'm using can call back my delgate function when 
        // an event occurs that it manages
        callbackFunction = NonAsync;
        SomeLibrary someLibrary = new SomeLibary();
        await someLibrary.SomeMethod(callbackFunction);
    }

    public static bool NonAsync()
    {
        // Do some work ...
        return false;
    }
}

// so now the callbackFunction is called from 
// the libary when some event occurs.  So that 
// means the actual method "NonAsync()" is called!
} 

The logic in the library that calls back this delgate looks like this

Example.Question.Library{
public ...{
    public ... SomeMethod(Delegate func){
        func.DynamicInvoke();
    }
}
}

this works just fine. What I need is to be able to call the Delegate from the library asynchronously.

need some help getting there

So i want something that looks like this...

...from the library which calls the delegate...

public ... SomeMethod(Delegate func){
        await func.DynamicInvoke();
    }

... from the consumer of the library which defines the delegate ...

Example.Question.Base{
    public static class Consumer{
        // ...

        public static async Task<T> Go(){
            // ...
            callbackFunction = await Async;
            // ...
        }

        public static async Task<bool> Async()
        {
            // Do some work ...
            return await Task.FromResult<bool>(false);
        }
    }
}

But the issue is I get a compiler error saying "Cannot await 'method group'"

enter image description here

like image 947
Robert Green MBA Avatar asked Sep 21 '25 11:09

Robert Green MBA


1 Answers

Just use something like that for example:

public static class Consumer
    {

        public static async Task<T> Go<T>()
        {
            var someLibrary = new SomeLibrary();
            return await someLibrary.SomeMethod(Async<T>);
        }

        public static async Task<T> Async<T>()
        {
            return await Task.FromResult(default(T));
        }
    }

public class SomeLibrary
    {
        public async Task<T> SomeMethod<T>(Func<Task<T>> func)
        {
            return await func.Invoke();
        }
    }
like image 194
Lukasz Ożóg Avatar answered Sep 23 '25 01:09

Lukasz Ożóg