src/THNETII.EtoForms.Hosting/EtoFormsCommandHandler.cs
/// <summary>
/// Creates a default Eto.Forms handler that runs a
/// <see cref="Eto.Forms.Application"/> singleton instance on the main
/// thread, while scheduling the execution of a specified handler as
/// a long-running task on a separate thread.
/// </summary>
/// <param name="handler">The handler to wrap, or <see langword="null"/> if no handler should be wrapped.</param>
/// <returns>A <see cref="ICommandHandler"/> instance that runs both the <see cref="Eto.Forms.Application"/> and the <paramref name="handler"/>.</returns>
Creates a default Eto.Forms handler that runs a
singleton instance on the main
thread, while scheduling the execution of a specified handler as
a long-running task on a separate thread.
Creates a default Eto . Forms handler that runs a singleton instance on the main thread while scheduling the execution of a specified handler as a long - running task on a separate thread .
public static ICommandHandler RunEtoFormsApplication(this ICommandHandler handler)
        {
            return CommandHandler.Create((IHost host, CancellationToken cancelToken) =>
            {
                var application = host.Services.GetRequiredService<Eto.Forms.Application>();
                var options = host.Services.GetRequiredService<IOptions<EtoFormsOptions>>().Value;
                object form = null;
                if (options.MainForm is Type formType)
                    form = ActivatorUtilities.GetServiceOrCreateInstance(
                        host.Services, formType);
                using var cancelReg = cancelToken.Register(obj =>
                {
                    var app = (Eto.Forms.Application)obj;
                    app.Quit();
                }, application);
                var handlerTask = handler is null ? null
                    : Task.Factory.StartNew(obj =>
                    {
                        var (handler, ctx) = (ValueTuple<ICommandHandler, InvocationContext>)obj;
                        return handler.InvokeAsync(ctx).GetAwaiter().GetResult();
                    },
                    (handler, host.Services.GetRequiredService<InvocationContext>()),
                    CancellationToken.None,
                    TaskCreationOptions.LongRunning,
                    Task.Factory.Scheduler);
                switch (form)
                {
                    case Eto.Forms.Form mainForm:
                        application.Run(mainForm);
                        break;
                    case Eto.Forms.Dialog dialog:
                        application.Run(dialog);
                        break;
                    case null:
                        application.Run();
                        break;
                }
                var hostLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
                hostLifetime.StopApplication();
                return handlerTask?.GetAwaiter().GetResult() ?? 0;
            });
        }
public static ICommandHandler RunEtoFormsApplication ( this ICommandHandler handler ) { return CommandHandler . Create ( ( IHost host , CancellationToken cancelToken ) => { var application = host . Services . GetRequiredService < Eto . Forms . Application > ( ) ; var options = host . Services . GetRequiredService < IOptions < EtoFormsOptions > > ( ) . Value ; object form = null ; if ( options . MainForm is Type formType ) form = ActivatorUtilities . GetServiceOrCreateInstance ( host . Services , formType ) ; using var cancelReg = cancelToken . Register ( obj => { var app = ( Eto . Forms . Application ) obj ; app . Quit ( ) ; } , application ) ; var handlerTask = handler is null ? null : Task . Factory . StartNew ( obj => { var ( handler , ctx ) = ( ValueTuple < ICommandHandler , InvocationContext > ) obj ; return handler . InvokeAsync ( ctx ) . GetAwaiter ( ) . GetResult ( ) ; } , ( handler , host . Services . GetRequiredService < InvocationContext > ( ) ) , CancellationToken . None , TaskCreationOptions . LongRunning , Task . Factory . Scheduler ) ; switch ( form ) { case Eto . Forms . Form mainForm : application . Run ( mainForm ) ; break ; case Eto . Forms . Dialog dialog : application . Run ( dialog ) ; break ; case null : application . Run ( ) ; break ; } var hostLifetime = host . Services . GetRequiredService < IHostApplicationLifetime > ( ) ; hostLifetime . StopApplication ( ) ; return handlerTask ? . GetAwaiter ( ) . GetResult ( ) ?? 0 ; } ) ; }
Creates a default Eto.Forms handler that runs a
singleton instance on the main
thread, while scheduling the execution of a specified handler as
a long-running task on a separate thread.
Creates a default Eto . Forms handler that runs a singleton instance on the main thread while scheduling the execution of a specified handler as a long - running task on a separate thread .
A  instance that runs both the  and the .
A instance that runs both the and the .
ICommandHandler
this
handler
The handler to wrap, or  if no handler should be wrapped.
The handler to wrap or if no handler should be wrapped .