Skip to content

Question about FireAndForget #4

@fretje

Description

@fretje

Hi,

I stumbled upon your code, and found that I have a very similar way of doing a FireAndForget as you have in this library. Only, I don't have the body of the Task.Run delegate wrapped into another invokation. So like this:

    public static void FireAndForget(
        Func<Task> task,
        Action<Exception> handle = null)
    {
        Task.Run(async () =>
        {
            try
            {
                await task().ConfigureAwait(false);
            }
            catch (Exception e)
            {
                if (null != handle)
                {
                    handle(e);
                }
            }
        });
    }

I have the above code running fine in production, but am always looking to "harden" it anyways, so I was just wandering: why do you do that? What is the reason of that extra level of invokation?

Also: about the ConfigureAwait... I see that you don't use that. If my understanding about it is right, applying ConfigureAwait(false) means that the synchronisation context doesn't get captured for continuation, which is good here, as we don't need to continue anywhere anyway.

I read somewhere that in library or framework code (I guess this project falls under that category?), ConfigureAwait (false) should almost always be used, as it's more expensive to capture than not to capture, and if you don't need it anyway...

I could be terribly wrong though, as I'm still in the process of wrapping my brain around this whole async/await stuff... Please enlighten me if that's the case ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions