aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
552 stars 312 forks source link

HostBuilder and headed UWP app on IoT core #1578

Closed tymtam2 closed 5 years ago

tymtam2 commented 5 years ago

What is the correct way to run IHost in a headed UWP App on IoT Core to avoid issues with running code on the UI thread?

I have to have something working quickly so for now I went for Thread.Start.

internal sealed partial class App : Windows.UI.Xaml.Application
{
    (...)
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        (...)
        new System.Threading.Thread(() =>
        {
            System.Threading.Thread.CurrentThread.IsBackground = true;
            MyRun();
        }).Start();
    }

    public void MyRun()
    {
        (...) 
        Host = ConfigureHost().Build();
        Host.Run();
    }

    private IHostBuilder ConfigureHost()
    {
        return new HostBuilder()
            .ConfigureHostConfiguration...
            .ConfigureAppConfiguration...
    }
}

Is this a correct way? Is there a better way?

davidfowl commented 5 years ago

Are you just trying to avoid blocking on the UI thread until the host shuts down? You can start Start on the host instead of Run, then you can call Dispose in the dispose of the Windows.UI.Xaml.Application (or close, not sure which one exists).

muratg commented 5 years ago

Hi. We're closing this issue as no response or updates have been provided in a timely manner and we have been unable to reproduce it. If you have more details and are encountering this issue please add a new reply and re-open the issue.

tymtam2 commented 5 years ago

We've stayed with

        {
            System.Threading.Thread.CurrentThread.IsBackground = true;
            MyRun();
        }).Start();

solution, but moved from OnLaunched since OnLaunched is called multiple times.

davidfowl commented 5 years ago

But why?