Skip to content

await PublishOnBackgroundThreadAsync(...) doesn't actually await the tasks #993

@dbruning

Description

@dbruning

I've been working under the assumption that if I await a call to PublishOnBackgroundThreadAsync, then I'm actually awaiting for all the handlers to run on all the subscribers. It turns out that's not the case. If it was a bad assumption on my part, then maybe the docs could clarify, or maybe it's just a bug?

Here's the current version:

        public static Task PublishOnBackgroundThreadAsync(this IEventAggregator eventAggregator, object message, CancellationToken cancellationToken)
        {
            return eventAggregator.PublishAsync(
			message, 
			f => Task.Factory.StartNew(f, default, TaskCreationOptions.None, TaskScheduler.Default), 
			cancellationToken
		);
        }

Here's a version which DOES wait for all the handlers to run:

	public static Task PublishOnBackgroundThreadAsync_WithUnwrap(this IEventAggregator eventAggregator, object message, CancellationToken cancellationToken)
	{
		return eventAggregator.PublishAsync(
			message,
			f => Task.Factory.StartNew(f, default, TaskCreationOptions.None, TaskScheduler.Default).Unwrap(),
			cancellationToken
		);
	}

Note the .Unwrap() as the only difference. Basically the original version is returning a Task<Task>

Thanks for your work on this library!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions