Skip to content

Run on same thread in generic host #195

@vlm---

Description

@vlm---

I was playing with new version and run into some trouble because my code wasn't executed on UI thread. It lead to code in https://github.com/natemcmaster/CommandLineUtils/blob/master/src/Hosting.CommandLine/Internal/CommandLineService.cs class and this method:

public Task<int> RunAsync(CancellationToken cancellationToken)
{
     _logger.LogDebug("Running");
     return Task.Run(() => _state.ExitCode = _application.Execute(_state.Arguments), cancellationToken);
}

It uses Task.Run to execute application login on different thread which is not desired in certain scenarios where you want your code to execute on the UI thread.
It can be easily rewritten to:

public Task<int> RunAsync(CancellationToken cancellationToken)
{
     _logger.LogDebug("Running");
     _state.ExitCode = _application.Execute(_state.Arguments);
     return Task.FromResult(_state.ExitCode);
}

That way the application logic is always executed on same thread. Would you be interested in PR for this change?

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions