From 6b05131cea829f949ead091995c28214f05daec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Tue, 26 Oct 2021 08:47:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4winform=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 7 +- .../FastGithub.Configuration.csproj | 1 + .../FastGithub.DomainResolve.csproj | 1 + FastGithub.Http/FastGithub.Http.csproj | 1 + .../FastGithub.HttpServer.csproj | 1 + .../FastGithub.PacketIntercept.csproj | 1 + FastGithub.Upgrade/FastGithub.Upgrade.csproj | 7 -- FastGithub.Windows/FastGithub.Windows.csproj | 13 --- FastGithub.Windows/Forms/MainForm.Designer.cs | 47 ---------- FastGithub.Windows/Forms/MainForm.cs | 20 ----- FastGithub.Windows/Forms/MainForm.resx | 60 ------------- .../Hosting/ApplicationOptions.cs | 25 ------ .../Hosting/IWinFormDispatcher.cs | 51 ----------- .../Hosting/WinFormDispatcher.cs | 15 ---- .../Hosting/WinFormHostBuilderExtensions.cs | 63 ------------- .../Hosting/WinFormHostLifetime.cs | 55 ------------ .../Hosting/WinFormHostedService.cs | 88 ------------------- .../ServiceCollectionExtensions.cs | 22 ----- FastGithub.sln | 12 --- FastGithub/AppHostedService.cs | 1 - FastGithub/FastGithub.csproj | 23 ++--- .../ProductionVersion.cs | 2 +- FastGithub/Program.cs | 8 +- 23 files changed, 16 insertions(+), 508 deletions(-) delete mode 100644 FastGithub.Upgrade/FastGithub.Upgrade.csproj delete mode 100644 FastGithub.Windows/FastGithub.Windows.csproj delete mode 100644 FastGithub.Windows/Forms/MainForm.Designer.cs delete mode 100644 FastGithub.Windows/Forms/MainForm.cs delete mode 100644 FastGithub.Windows/Forms/MainForm.resx delete mode 100644 FastGithub.Windows/Hosting/ApplicationOptions.cs delete mode 100644 FastGithub.Windows/Hosting/IWinFormDispatcher.cs delete mode 100644 FastGithub.Windows/Hosting/WinFormDispatcher.cs delete mode 100644 FastGithub.Windows/Hosting/WinFormHostBuilderExtensions.cs delete mode 100644 FastGithub.Windows/Hosting/WinFormHostLifetime.cs delete mode 100644 FastGithub.Windows/Hosting/WinFormHostedService.cs delete mode 100644 FastGithub.Windows/ServiceCollectionExtensions.cs rename {FastGithub.Upgrade => FastGithub}/ProductionVersion.cs (99%) diff --git a/Directory.Build.props b/Directory.Build.props index ebc26e3..b077ab9 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,12 +1,9 @@ - 2.0.5 - enable + 2.0.5 github加速神器 https://github.com/dotnetcore/FastGithub - win-x64 - true - true + win-x64 diff --git a/FastGithub.Configuration/FastGithub.Configuration.csproj b/FastGithub.Configuration/FastGithub.Configuration.csproj index b469e5e..4f9e152 100644 --- a/FastGithub.Configuration/FastGithub.Configuration.csproj +++ b/FastGithub.Configuration/FastGithub.Configuration.csproj @@ -1,6 +1,7 @@  net6.0 + enable diff --git a/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj b/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj index 2c92ada..1a84f5e 100644 --- a/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj +++ b/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj @@ -1,6 +1,7 @@  net6.0 + enable diff --git a/FastGithub.Http/FastGithub.Http.csproj b/FastGithub.Http/FastGithub.Http.csproj index ad52c1e..4516cfa 100644 --- a/FastGithub.Http/FastGithub.Http.csproj +++ b/FastGithub.Http/FastGithub.Http.csproj @@ -1,6 +1,7 @@  net6.0 + enable diff --git a/FastGithub.HttpServer/FastGithub.HttpServer.csproj b/FastGithub.HttpServer/FastGithub.HttpServer.csproj index 51b1802..f861854 100644 --- a/FastGithub.HttpServer/FastGithub.HttpServer.csproj +++ b/FastGithub.HttpServer/FastGithub.HttpServer.csproj @@ -2,6 +2,7 @@ net6.0 + enable true diff --git a/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj b/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj index e9fa703..d8f6d99 100644 --- a/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj +++ b/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj @@ -2,6 +2,7 @@ net6.0 + enable true diff --git a/FastGithub.Upgrade/FastGithub.Upgrade.csproj b/FastGithub.Upgrade/FastGithub.Upgrade.csproj deleted file mode 100644 index 0a94a8d..0000000 --- a/FastGithub.Upgrade/FastGithub.Upgrade.csproj +++ /dev/null @@ -1,7 +0,0 @@ - - - - net6.0 - - - diff --git a/FastGithub.Windows/FastGithub.Windows.csproj b/FastGithub.Windows/FastGithub.Windows.csproj deleted file mode 100644 index 1a723e1..0000000 --- a/FastGithub.Windows/FastGithub.Windows.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net6.0-windows - true - - - - - - - - diff --git a/FastGithub.Windows/Forms/MainForm.Designer.cs b/FastGithub.Windows/Forms/MainForm.Designer.cs deleted file mode 100644 index 0b8aeec..0000000 --- a/FastGithub.Windows/Forms/MainForm.Designer.cs +++ /dev/null @@ -1,47 +0,0 @@ -namespace FastGithub.Windows.Forms -{ - partial class MainForm - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.SuspendLayout(); - // - // MainForm - // - this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Name = "MainForm"; - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "FastGithub"; - this.ResumeLayout(false); - - } - - #endregion - } -} \ No newline at end of file diff --git a/FastGithub.Windows/Forms/MainForm.cs b/FastGithub.Windows/Forms/MainForm.cs deleted file mode 100644 index 864fec7..0000000 --- a/FastGithub.Windows/Forms/MainForm.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace FastGithub.Windows.Forms -{ - public partial class MainForm : Form - { - public MainForm() - { - InitializeComponent(); - } - } -} diff --git a/FastGithub.Windows/Forms/MainForm.resx b/FastGithub.Windows/Forms/MainForm.resx deleted file mode 100644 index f298a7b..0000000 --- a/FastGithub.Windows/Forms/MainForm.resx +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/FastGithub.Windows/Hosting/ApplicationOptions.cs b/FastGithub.Windows/Hosting/ApplicationOptions.cs deleted file mode 100644 index 1a95761..0000000 --- a/FastGithub.Windows/Hosting/ApplicationOptions.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Windows.Forms; - -namespace FastGithub.Windows.Hosting -{ - /// - /// 表示Application选项 - /// - public class ApplicationOptions - { - /// - /// 获取或设置是否启用VisualStyles - /// - public bool EnableVisualStyles { get; set; } = true; - - /// - /// 获取或设置高Dpi的模式 - /// - public HighDpiMode HighDpiMode { get; set; } = HighDpiMode.SystemAware; - - /// - /// 获取或设置是否兼容TextRendering - /// - public bool CompatibleTextRenderingDefault { get; set; } = false; - } -} diff --git a/FastGithub.Windows/Hosting/IWinFormDispatcher.cs b/FastGithub.Windows/Hosting/IWinFormDispatcher.cs deleted file mode 100644 index 6e74be7..0000000 --- a/FastGithub.Windows/Hosting/IWinFormDispatcher.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Threading; - -namespace FastGithub.Windows.Hosting -{ - /// - /// WinForm调度器 - /// - public interface IWinFormDispatcher - { - /// - /// 获取或设置同步上下文 - /// - SynchronizationContext? SynchronizationContext { get; set; } - - /// - /// 尝试在同步上下文投递执行委托 - /// - /// - /// - bool TryInvoke(Action action) - { - var context = this.SynchronizationContext; - if (context == null || action == null) - { - return false; - } - - context.Post(state => ((Action)state!)(), action); - return false; - } - - /// - /// 在同步上下文投递执行委托 - /// - /// - void Invoke(Action action) - { - var context = this.SynchronizationContext; - if (context == null) - { - throw new InvalidOperationException($"{nameof(SynchronizationContext)} is null"); - } - - if (action != null) - { - context.Post(state => ((Action)state!)(), action); - } - } - } -} diff --git a/FastGithub.Windows/Hosting/WinFormDispatcher.cs b/FastGithub.Windows/Hosting/WinFormDispatcher.cs deleted file mode 100644 index 9afecb5..0000000 --- a/FastGithub.Windows/Hosting/WinFormDispatcher.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Threading; - -namespace FastGithub.Windows.Hosting -{ - /// - /// WinForm调度器 - /// - sealed class WinFormDispatcher : IWinFormDispatcher - { - /// - /// 获取或设置同步上下文 - /// - public SynchronizationContext? SynchronizationContext { get; set; } - } -} diff --git a/FastGithub.Windows/Hosting/WinFormHostBuilderExtensions.cs b/FastGithub.Windows/Hosting/WinFormHostBuilderExtensions.cs deleted file mode 100644 index 3b4c959..0000000 --- a/FastGithub.Windows/Hosting/WinFormHostBuilderExtensions.cs +++ /dev/null @@ -1,63 +0,0 @@ -using FastGithub.Windows.Hosting; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Windows.Forms; - -namespace Microsoft.Extensions.Hosting -{ - /// - /// IHostBuilder的WinForm扩展 - /// - public static class WinFormHostBuilderExtensions - { - /// - /// 指定WinForm的主窗体 - /// - /// - /// * 该方法需要在services.AddHostedService()之前调用 - /// - /// - /// - /// - public static IHostBuilder UseWinForm(this IHostBuilder hostBuilder) where TMainForm : Form - { - return hostBuilder.ConfigureServices((context, services) => - { - services - .AddSingleton() - .AddSingleton() - .AddHostedService>(); - }); - } - - /// - /// 使用WinForm生命周期 - /// - /// - /// * 关闭主窗体或调用Appliaction.Exit()之后生命结束 - /// - /// - /// - public static IHostBuilder UseWinFormHostLifetime(this IHostBuilder hostBuilder) - { - return hostBuilder.UseWinFormHostLifetime(c => { }); - } - - /// - /// 使用WinForm生命周期 - /// - /// - /// * 关闭主窗体或调用Appliaction.Exit()之后生命结束 - /// - /// - /// Applicaiton选项 - public static IHostBuilder UseWinFormHostLifetime(this IHostBuilder hostBuilder, Action configureOptions) - { - return hostBuilder.ConfigureServices((context, services) => - { - services.Configure(configureOptions); - services.AddSingleton(); - }); - } - } -} diff --git a/FastGithub.Windows/Hosting/WinFormHostLifetime.cs b/FastGithub.Windows/Hosting/WinFormHostLifetime.cs deleted file mode 100644 index 91b1624..0000000 --- a/FastGithub.Windows/Hosting/WinFormHostLifetime.cs +++ /dev/null @@ -1,55 +0,0 @@ -using FastGithub.Windows.Hosting; -using Microsoft.Extensions.Options; -using System; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace Microsoft.Extensions.Hosting -{ - /// - /// WinForm生命周期 - /// - sealed class WinFormHostLifetime : IHostLifetime, IDisposable - { - private readonly IHostApplicationLifetime applicationLifetime; - private readonly IOptions applicationOptions; - - public WinFormHostLifetime(IHostApplicationLifetime applicationLifetime, IOptions applicationOptions) - { - this.applicationLifetime = applicationLifetime; - this.applicationOptions = applicationOptions; - } - - public Task WaitForStartAsync(CancellationToken cancellationToken) - { - var option = this.applicationOptions.Value; - if (option.EnableVisualStyles == true) - { - Application.EnableVisualStyles(); - } - - Application.SetHighDpiMode(option.HighDpiMode); - Application.SetCompatibleTextRenderingDefault(option.CompatibleTextRenderingDefault); - - Application.ApplicationExit += OnApplicationExit; - return Task.CompletedTask; - } - - private void OnApplicationExit(object? sender, System.EventArgs e) - { - Application.ApplicationExit -= OnApplicationExit; - this.applicationLifetime.StopApplication(); - } - - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } - - public void Dispose() - { - Application.ApplicationExit -= OnApplicationExit; - } - } -} diff --git a/FastGithub.Windows/Hosting/WinFormHostedService.cs b/FastGithub.Windows/Hosting/WinFormHostedService.cs deleted file mode 100644 index b7b4f48..0000000 --- a/FastGithub.Windows/Hosting/WinFormHostedService.cs +++ /dev/null @@ -1,88 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using System; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace FastGithub.Windows.Hosting -{ - /// - /// WinForm后台任务和WinForm线程 - /// - /// - sealed class WinFormHostedService : IHostedService where TMainForm : Form - { - private readonly Thread staThread; - private readonly IServiceProvider serviceProvider; - private readonly TaskCompletionSource taskCompletionSource = new(); - - /// - /// WinForm后台任务 - /// - /// - public WinFormHostedService(IServiceProvider serviceProvider) - { - this.serviceProvider = serviceProvider; - this.staThread = new Thread(StaRunMainFrom); - this.staThread.TrySetApartmentState(ApartmentState.STA); - } - - /// - /// 启动 - /// - /// - /// - public Task StartAsync(CancellationToken cancellationToken) - { - this.staThread.Start(); - return this.taskCompletionSource.Task; - } - - /// - /// STA线程 - /// - private void StaRunMainFrom() - { - try - { - var mainForm = this.CreateMainForm(); - this.taskCompletionSource.TrySetResult(); - Application.Run(mainForm); - } - catch (Exception ex) - { - this.taskCompletionSource.TrySetException(ex); - } - } - - /// - /// 实例化MainForm与初始化调度器 - /// - /// - private TMainForm CreateMainForm() - { - // 在STA线程实例化TMainForm,保证该线程拥有SynchronizationContext - var mainForm = this.serviceProvider.GetRequiredService(); - if (SynchronizationContext.Current is null) - { - throw new InvalidOperationException($"不允许在其它线程上实例化{typeof(TMainForm)}"); - } - - var dispatcher = this.serviceProvider.GetRequiredService(); - dispatcher.SynchronizationContext = SynchronizationContext.Current; - return mainForm; - } - - /// - /// 停止 - /// - /// - /// - public Task StopAsync(CancellationToken cancellationToken) - { - Application.Exit(); - return Task.CompletedTask; - } - } -} diff --git a/FastGithub.Windows/ServiceCollectionExtensions.cs b/FastGithub.Windows/ServiceCollectionExtensions.cs deleted file mode 100644 index 3c1cc8b..0000000 --- a/FastGithub.Windows/ServiceCollectionExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Logging; - -namespace FastGithub -{ - /// - /// 服务注册扩展 - /// - public static class ServiceCollectionExtensions - { - /// - /// 添加配置服务 - /// - /// - /// - public static IServiceCollection AddWinForm(this IServiceCollection services) - { - return services; - } - } -} diff --git a/FastGithub.sln b/FastGithub.sln index 8ade0bc..0e19d20 100644 --- a/FastGithub.sln +++ b/FastGithub.sln @@ -15,10 +15,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.HttpServer", "Fa EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.PacketIntercept", "FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj", "{701FF90C-E651-4E0B-AE7F-84D1F17DD178}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Windows", "FastGithub.Windows\FastGithub.Windows.csproj", "{4F9914B7-2519-46F9-96F1-42400B6BFD6E}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastGithub.Upgrade", "FastGithub.Upgrade\FastGithub.Upgrade.csproj", "{D644B53B-91E4-41DD-818B-FE3A77ED8D10}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -49,14 +45,6 @@ Global {701FF90C-E651-4E0B-AE7F-84D1F17DD178}.Debug|Any CPU.Build.0 = Debug|Any CPU {701FF90C-E651-4E0B-AE7F-84D1F17DD178}.Release|Any CPU.ActiveCfg = Release|Any CPU {701FF90C-E651-4E0B-AE7F-84D1F17DD178}.Release|Any CPU.Build.0 = Release|Any CPU - {4F9914B7-2519-46F9-96F1-42400B6BFD6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4F9914B7-2519-46F9-96F1-42400B6BFD6E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4F9914B7-2519-46F9-96F1-42400B6BFD6E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4F9914B7-2519-46F9-96F1-42400B6BFD6E}.Release|Any CPU.Build.0 = Release|Any CPU - {D644B53B-91E4-41DD-818B-FE3A77ED8D10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D644B53B-91E4-41DD-818B-FE3A77ED8D10}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D644B53B-91E4-41DD-818B-FE3A77ED8D10}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D644B53B-91E4-41DD-818B-FE3A77ED8D10}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FastGithub/AppHostedService.cs b/FastGithub/AppHostedService.cs index 796cdfe..7baf2fc 100644 --- a/FastGithub/AppHostedService.cs +++ b/FastGithub/AppHostedService.cs @@ -1,5 +1,4 @@ using FastGithub.Configuration; -using FastGithub.Upgrade; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 7f8f335..b94f585 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -1,27 +1,17 @@ - + - net6.0;net6.0-windows + net6.0 + enable fastgithub Exe MIT true app.ico app.manifest - - - - true - WinExe - - - - - - - - - + true + true + @@ -29,7 +19,6 @@ - diff --git a/FastGithub.Upgrade/ProductionVersion.cs b/FastGithub/ProductionVersion.cs similarity index 99% rename from FastGithub.Upgrade/ProductionVersion.cs rename to FastGithub/ProductionVersion.cs index 5a757d1..2b8fb70 100644 --- a/FastGithub.Upgrade/ProductionVersion.cs +++ b/FastGithub/ProductionVersion.cs @@ -2,7 +2,7 @@ using System.Reflection; using System.Text.RegularExpressions; -namespace FastGithub.Upgrade +namespace FastGithub { /// /// 表示产品版本 diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 13947b6..de8ca5c 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -26,12 +26,8 @@ namespace FastGithub public static IHostBuilder CreateHostBuilder(string[] args) { return Host - .CreateDefaultBuilder(args) -#if WINDOWS - .UseWindowsService() - .UseWinForm() - .UseWinFormHostLifetime() -#endif + .CreateDefaultBuilder(args) + .UseWindowsService() .UseDefaultServiceProvider(c => { c.ValidateOnBuild = false;