集成winform
This commit is contained in:
parent
33197ec838
commit
299e9d706e
@ -5,7 +5,6 @@
|
|||||||
<Description>github加速神器</Description>
|
<Description>github加速神器</Description>
|
||||||
<Copyright>https://github.com/dotnetcore/FastGithub</Copyright>
|
<Copyright>https://github.com/dotnetcore/FastGithub</Copyright>
|
||||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
|
||||||
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
|
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
|
||||||
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
|
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
|
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
7
FastGithub.Upgrade/FastGithub.Upgrade.csproj
Normal file
7
FastGithub.Upgrade/FastGithub.Upgrade.csproj
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@ -2,7 +2,7 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub.Upgrade
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 表示产品版本
|
/// 表示产品版本
|
||||||
13
FastGithub.Windows/FastGithub.Windows.csproj
Normal file
13
FastGithub.Windows/FastGithub.Windows.csproj
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0-windows</TargetFramework>
|
||||||
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
47
FastGithub.Windows/Forms/MainForm.Designer.cs
generated
Normal file
47
FastGithub.Windows/Forms/MainForm.Designer.cs
generated
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
namespace FastGithub.Windows.Forms
|
||||||
|
{
|
||||||
|
partial class MainForm
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Required designer variable.
|
||||||
|
/// </summary>
|
||||||
|
private System.ComponentModel.IContainer components = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clean up any resources being used.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||||
|
protected override void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (disposing && (components != null))
|
||||||
|
{
|
||||||
|
components.Dispose();
|
||||||
|
}
|
||||||
|
base.Dispose(disposing);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Windows Form Designer generated code
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Required method for Designer support - do not modify
|
||||||
|
/// the contents of this method with the code editor.
|
||||||
|
/// </summary>
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
20
FastGithub.Windows/Forms/MainForm.cs
Normal file
20
FastGithub.Windows/Forms/MainForm.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
60
FastGithub.Windows/Forms/MainForm.resx
Normal file
60
FastGithub.Windows/Forms/MainForm.resx
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<root>
|
||||||
|
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||||
|
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||||
|
<xsd:element name="root" msdata:IsDataSet="true">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:choice maxOccurs="unbounded">
|
||||||
|
<xsd:element name="metadata">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="assembly">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:attribute name="alias" type="xsd:string" />
|
||||||
|
<xsd:attribute name="name" type="xsd:string" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="data">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="resheader">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:choice>
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:schema>
|
||||||
|
<resheader name="resmimetype">
|
||||||
|
<value>text/microsoft-resx</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="version">
|
||||||
|
<value>2.0</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="reader">
|
||||||
|
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="writer">
|
||||||
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
</root>
|
||||||
25
FastGithub.Windows/Hosting/ApplicationOptions.cs
Normal file
25
FastGithub.Windows/Hosting/ApplicationOptions.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
|
namespace FastGithub.Windows.Hosting
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 表示Application选项
|
||||||
|
/// </summary>
|
||||||
|
public class ApplicationOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置是否启用VisualStyles
|
||||||
|
/// </summary>
|
||||||
|
public bool EnableVisualStyles { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置高Dpi的模式
|
||||||
|
/// </summary>
|
||||||
|
public HighDpiMode HighDpiMode { get; set; } = HighDpiMode.SystemAware;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置是否兼容TextRendering
|
||||||
|
/// </summary>
|
||||||
|
public bool CompatibleTextRenderingDefault { get; set; } = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
51
FastGithub.Windows/Hosting/IWinFormDispatcher.cs
Normal file
51
FastGithub.Windows/Hosting/IWinFormDispatcher.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace FastGithub.Windows.Hosting
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// WinForm调度器
|
||||||
|
/// </summary>
|
||||||
|
public interface IWinFormDispatcher
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置同步上下文
|
||||||
|
/// </summary>
|
||||||
|
SynchronizationContext? SynchronizationContext { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尝试在同步上下文投递执行委托
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="action"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool TryInvoke(Action action)
|
||||||
|
{
|
||||||
|
var context = this.SynchronizationContext;
|
||||||
|
if (context == null || action == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Post(state => ((Action)state!)(), action);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 在同步上下文投递执行委托
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="action"></param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
FastGithub.Windows/Hosting/WinFormDispatcher.cs
Normal file
15
FastGithub.Windows/Hosting/WinFormDispatcher.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace FastGithub.Windows.Hosting
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// WinForm调度器
|
||||||
|
/// </summary>
|
||||||
|
sealed class WinFormDispatcher : IWinFormDispatcher
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置同步上下文
|
||||||
|
/// </summary>
|
||||||
|
public SynchronizationContext? SynchronizationContext { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
63
FastGithub.Windows/Hosting/WinFormHostBuilderExtensions.cs
Normal file
63
FastGithub.Windows/Hosting/WinFormHostBuilderExtensions.cs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
using FastGithub.Windows.Hosting;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
|
namespace Microsoft.Extensions.Hosting
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// IHostBuilder的WinForm扩展
|
||||||
|
/// </summary>
|
||||||
|
public static class WinFormHostBuilderExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 指定WinForm的主窗体
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// * 该方法需要在services.AddHostedService()之前调用
|
||||||
|
/// </remarks>
|
||||||
|
/// <typeparam name="TMainForm"></typeparam>
|
||||||
|
/// <param name="hostBuilder"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IHostBuilder UseWinForm<TMainForm>(this IHostBuilder hostBuilder) where TMainForm : Form
|
||||||
|
{
|
||||||
|
return hostBuilder.ConfigureServices((context, services) =>
|
||||||
|
{
|
||||||
|
services
|
||||||
|
.AddSingleton<TMainForm>()
|
||||||
|
.AddSingleton<IWinFormDispatcher, WinFormDispatcher>()
|
||||||
|
.AddHostedService<WinFormHostedService<TMainForm>>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用WinForm生命周期
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// * 关闭主窗体或调用Appliaction.Exit()之后生命结束
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="hostBuilder"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IHostBuilder UseWinFormHostLifetime(this IHostBuilder hostBuilder)
|
||||||
|
{
|
||||||
|
return hostBuilder.UseWinFormHostLifetime(c => { });
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用WinForm生命周期
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// * 关闭主窗体或调用Appliaction.Exit()之后生命结束
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="hostBuilder"></param>
|
||||||
|
/// <param name="configureOptions">Applicaiton选项</param>
|
||||||
|
public static IHostBuilder UseWinFormHostLifetime(this IHostBuilder hostBuilder, Action<ApplicationOptions> configureOptions)
|
||||||
|
{
|
||||||
|
return hostBuilder.ConfigureServices((context, services) =>
|
||||||
|
{
|
||||||
|
services.Configure(configureOptions);
|
||||||
|
services.AddSingleton<IHostLifetime, WinFormHostLifetime>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
55
FastGithub.Windows/Hosting/WinFormHostLifetime.cs
Normal file
55
FastGithub.Windows/Hosting/WinFormHostLifetime.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// WinForm生命周期
|
||||||
|
/// </summary>
|
||||||
|
sealed class WinFormHostLifetime : IHostLifetime, IDisposable
|
||||||
|
{
|
||||||
|
private readonly IHostApplicationLifetime applicationLifetime;
|
||||||
|
private readonly IOptions<ApplicationOptions> applicationOptions;
|
||||||
|
|
||||||
|
public WinFormHostLifetime(IHostApplicationLifetime applicationLifetime, IOptions<ApplicationOptions> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
88
FastGithub.Windows/Hosting/WinFormHostedService.cs
Normal file
88
FastGithub.Windows/Hosting/WinFormHostedService.cs
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// WinForm后台任务和WinForm线程
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TMainForm"></typeparam>
|
||||||
|
sealed class WinFormHostedService<TMainForm> : IHostedService where TMainForm : Form
|
||||||
|
{
|
||||||
|
private readonly Thread staThread;
|
||||||
|
private readonly IServiceProvider serviceProvider;
|
||||||
|
private readonly TaskCompletionSource taskCompletionSource = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WinForm后台任务
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serviceProvider"></param>
|
||||||
|
public WinFormHostedService(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
this.serviceProvider = serviceProvider;
|
||||||
|
this.staThread = new Thread(StaRunMainFrom);
|
||||||
|
this.staThread.TrySetApartmentState(ApartmentState.STA);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启动
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.staThread.Start();
|
||||||
|
return this.taskCompletionSource.Task;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// STA线程
|
||||||
|
/// </summary>
|
||||||
|
private void StaRunMainFrom()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var mainForm = this.CreateMainForm();
|
||||||
|
this.taskCompletionSource.TrySetResult();
|
||||||
|
Application.Run(mainForm);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.taskCompletionSource.TrySetException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实例化MainForm与初始化调度器
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="InvalidOperationException"></exception>
|
||||||
|
private TMainForm CreateMainForm()
|
||||||
|
{
|
||||||
|
// 在STA线程实例化TMainForm,保证该线程拥有SynchronizationContext
|
||||||
|
var mainForm = this.serviceProvider.GetRequiredService<TMainForm>();
|
||||||
|
if (SynchronizationContext.Current is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"不允许在其它线程上实例化{typeof(TMainForm)}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var dispatcher = this.serviceProvider.GetRequiredService<IWinFormDispatcher>();
|
||||||
|
dispatcher.SynchronizationContext = SynchronizationContext.Current;
|
||||||
|
return mainForm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 停止
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
Application.Exit();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
FastGithub.Windows/ServiceCollectionExtensions.cs
Normal file
22
FastGithub.Windows/ServiceCollectionExtensions.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace FastGithub
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 服务注册扩展
|
||||||
|
/// </summary>
|
||||||
|
public static class ServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 添加配置服务
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IServiceCollection AddWinForm(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -15,6 +15,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.HttpServer", "Fa
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.PacketIntercept", "FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj", "{701FF90C-E651-4E0B-AE7F-84D1F17DD178}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.PacketIntercept", "FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj", "{701FF90C-E651-4E0B-AE7F-84D1F17DD178}"
|
||||||
EndProject
|
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
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -45,6 +49,14 @@ Global
|
|||||||
{701FF90C-E651-4E0B-AE7F-84D1F17DD178}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{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.ActiveCfg = Release|Any CPU
|
||||||
{701FF90C-E651-4E0B-AE7F-84D1F17DD178}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using FastGithub.Configuration;
|
using FastGithub.Configuration;
|
||||||
|
using FastGithub.Upgrade;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
<TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>
|
||||||
<AssemblyName>fastgithub</AssemblyName>
|
<AssemblyName>fastgithub</AssemblyName>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
@ -9,12 +10,26 @@
|
|||||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0-windows'">
|
||||||
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-windows'">
|
||||||
|
<ProjectReference Include="..\FastGithub.Windows\FastGithub.Windows.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
|
<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
|
||||||
<ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />
|
<ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />
|
||||||
<ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" />
|
<ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" />
|
||||||
<ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" />
|
<ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" />
|
||||||
|
<ProjectReference Include="..\FastGithub.Upgrade\FastGithub.Upgrade.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -27,7 +27,11 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
return Host
|
return Host
|
||||||
.CreateDefaultBuilder(args)
|
.CreateDefaultBuilder(args)
|
||||||
|
#if WINDOWS
|
||||||
.UseWindowsService()
|
.UseWindowsService()
|
||||||
|
.UseWinForm<Windows.Forms.MainForm>()
|
||||||
|
.UseWinFormHostLifetime()
|
||||||
|
#endif
|
||||||
.UseDefaultServiceProvider(c =>
|
.UseDefaultServiceProvider(c =>
|
||||||
{
|
{
|
||||||
c.ValidateOnBuild = false;
|
c.ValidateOnBuild = false;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user