Skip to content

Commit

Permalink
Aria2 Process running update
Browse files Browse the repository at this point in the history
Aria2 Process running update
  • Loading branch information
Gaoyifei1011 committed Sep 20, 2022
1 parent c4fe06e commit 9e0f50b
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Description/README_EN-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Note: The app can't bypass the Microsoft Store's billing channels, and if the ap

### Download and installation considerations

> * The program is built using the Windows Apps SDK, and it is recommended that your system version be Windows 11 (codename 21H2 / build 22000) or later, and the minimum version is Windows 10 (codename 1709 / build 17763) or later.
> * The program is built using the Windows Apps SDK, and it is recommended that your system version be Windows 11 (codename 21H2 / build 22000) or later, and the minimum version is Windows 10 (codename 1803 / build 18362) or later.
> * If your system is Windows 10, there are some limitations to app functionality:
Setting the mica/acrylic background color is not supported at this time
The application part of the icon uses the Segoe Fluent Icons icon, this type of icon is not built into the Windows icon, so there will be icon anomalies when opening the application for the first time. You need to download the corresponding [icon file](https://docs.microsoft.com/zh-cn/windows/apps/design/downloads/#fonts) yourself, click the right-click menu to install the font icon file, and restart the application icon to display normally.
Expand Down
2 changes: 1 addition & 1 deletion Description/README_ZH-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

### 下载与安装注意事项

> * 该程序使用的是Windows 应用 SDK构建的,建议您的系统版本为Windows 11(代号 21H2 / 内部版本号 22000)或更高版本,最低版本为Windows 10(代号1709 / 内部版本号17763)或更高版本。
> * 该程序使用的是Windows 应用 SDK构建的,建议您的系统版本为Windows 11(代号 21H2 / 内部版本号 22000)或更高版本,最低版本为Windows 10(代号1803 / 内部版本号18362)或更高版本。
> * 如果您的系统是Windows 10,应用功能存在一些限制:
暂不支持设置云母/亚克力背景色
应用部分图标使用的是Segoe Fluent Icons图标,这一类型图标并没有内置到Windows图标,所以初次打开应用时会存在图标异常的问题。需要您亲自下载相应的[图标文件](https://docs.microsoft.com/zh-cn/windows/apps/design/downloads/#fonts),点击右键菜单安装该字体图标文件,重启应用图标才能正常显示。
Expand Down
2 changes: 1 addition & 1 deletion GetStoreApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void AppActivated(object sender, AppActivationArguments e)
{
WindowHelper.SetAppWindow();

//显示提示消息对话框
// 显示提示消息对话框
WinUIMessageBox.ShowMessageBox(
(HWND)WinRT.Interop.WindowNative.GetWindowHandle(MainWindow),
ResourceService.GetLocalized("AppIsRunning"),
Expand Down
7 changes: 2 additions & 5 deletions GetStoreApp/GetStoreApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<TargetPlatformMinVersion>10.0.18362.0</TargetPlatformMinVersion>
<RootNamespace>GetStoreApp</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>Properties\PublishProfiles\win10-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<PackageCertificateThumbprint>7F64DB0B0770C29C61E5F8DC60430B3DC390D99F</PackageCertificateThumbprint>
<WindowsAppSDKSelfContained>false</WindowsAppSDKSelfContained>
<ApplicationIcon>Assets\Logo\GetStoreApp.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>0.5.0.0</Version>
<FileVersion>0.5.0.0</FileVersion>
<SupportedOSPlatformVersion>10.0.18362.0</SupportedOSPlatformVersion>
<Copyright>高怡飞</Copyright>
<AppxPackageSigningTimestampDigestAlgorithm>SHA256</AppxPackageSigningTimestampDigestAlgorithm>
<GenerateTestArtifacts>True</GenerateTestArtifacts>
<StartupObject>GetStoreApp.Program</StartupObject>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -103,7 +101,6 @@
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.1" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.5" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.8" />
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
<PackageReference Include="PInvoke.SHCore" Version="0.7.124" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="WinUIEx" Version="1.8.0" />
Expand Down
97 changes: 38 additions & 59 deletions GetStoreApp/Helpers/Aria2ProcessHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Threading.Tasks;

Expand All @@ -14,48 +11,40 @@ namespace GetStoreApp.Helpers
/// </summary>
public static class Aria2ProcessHelper
{
private static Process Aria2Process { get; } = new Process();

private static List<int> Aria2ProcessList { get; } = new List<int>();
// Aria2下载进程的ID号
private static int Aria2ProcessID = default;

/// <summary>
/// 获取当前进程的PID信息
/// </summary>
public static int GetProcessID()
{
return Aria2Process.Id;
return Aria2ProcessID;
}

/// <summary>
/// 启动命令提示符进程
/// 让Aria2以RPC方式启动,并让其在后台运行
/// </summary>
public static async Task RunCmdAsync()
public static async Task RunAria2Async(string fileName, string arguments)
{
Aria2Process.StartInfo.FileName = "cmd.exe";
Aria2Process.StartInfo.UseShellExecute = false;
Aria2Process.StartInfo.RedirectStandardInput = true;
Aria2Process.StartInfo.RedirectStandardOutput = true;
Aria2Process.StartInfo.RedirectStandardError = true;
Aria2Process.StartInfo.CreateNoWindow = true;
Aria2Process.Start();
//设置启动程序的信息
ProcessStartInfo Aria2Info = new ProcessStartInfo();

await Task.CompletedTask;
}
//设置外部程序名
Aria2Info.FileName = fileName;

/// <summary>
/// 命令提示符进程运行后,输入Aria2 RPC方式执行命令,并让其在后台运行
/// </summary>
public static async Task ExecuteCmdAsync(string executeCmd)
{
StreamWriter ConhostWriter = Aria2Process.StandardInput;
Aria2Process.BeginOutputReadLine();
if (!string.IsNullOrEmpty(executeCmd))
{
ConhostWriter.WriteLine(executeCmd);
}
ConhostWriter.Close();
Aria2Info.RedirectStandardOutput = true;
Aria2Info.RedirectStandardError = true;
Aria2Info.UseShellExecute = false;
Aria2Info.CreateNoWindow = true;

await Aria2Process.WaitForExitAsync();
//最小化方式启动
Aria2Info.WindowStyle = ProcessWindowStyle.Hidden;
Aria2Info.Arguments = arguments;

// 启动Aria2下载进程,并设置进程ID号
Aria2ProcessID = Process.Start(Aria2Info).Id;
await Task.CompletedTask;
}

/// <summary>
Expand All @@ -66,7 +55,7 @@ public static void KillProcessAndChildren(int processID)
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + processID);
ManagementObjectCollection moc = searcher.Get();

foreach (ManagementObject mo in moc)
foreach (ManagementBaseObject mo in moc)
{
KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
}
Expand All @@ -78,50 +67,40 @@ public static void KillProcessAndChildren(int processID)
}

// 无法终止相关联的进程。
catch (Win32Exception) { }
catch (Win32Exception)
{
return;
}

// 不支持远程计算机上运行的进程调用 Kill()。 该方法仅对本地计算机上运行的进程可用。
catch (NotSupportedException) { }
catch (NotSupportedException)
{
return;
}
// 没有与此 Process 对象关联的进程
catch (InvalidOperationException) { }
catch (InvalidOperationException)
{
return;
}
}

/// <summary>
/// 判断Aria2进程是否存活
/// </summary>
public static bool IsAria2ProcessExisted()
{
if (Aria2ProcessList.Count == 0)
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + GetProcessID());
ManagementObjectCollection moc = searcher.Get();

foreach (ManagementObject mo in moc.Cast<ManagementObject>())
if (GetProcessID() != default)
{
Aria2ProcessList.Add(Convert.ToInt32(mo["ProcessID"]));
Process.GetProcessById(GetProcessID());
return true;
}
}

foreach (int processId in Aria2ProcessList)
{
if (!IsProcessRunning(processId))
else
{
return false;
}
}
return true;
}

/// <summary>
/// 检测进程是否正在运行
/// </summary>
private static bool IsProcessRunning(int id)
{
try
{
Process.GetProcessById(id);
return true;
}
// 无法终止相关联的进程。
catch (Win32Exception)
{
Expand Down
9 changes: 3 additions & 6 deletions GetStoreApp/Services/Download/Aria2Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ namespace GetStoreApp.Services.Download
/// </summary>
public class Aria2Service : IAria2Service
{
private string Aria2Path => Path.Combine(AppContext.BaseDirectory, "Aria2\\GetStoreAppAria2.exe");
private string Aria2FileName => Path.Combine(AppContext.BaseDirectory, "Aria2\\GetStoreAppAria2.exe");

private string Aria2ConfPath => Path.Combine(AppContext.BaseDirectory, "Aria2\\Config\\aria2.conf");

private string Aria2ExecuteCmd => string.Format("{0} --conf-path=\"{1}\" -D", Aria2Path, Aria2ConfPath);
private string Aria2Arguments => string.Format("--conf-path=\"{0}\" -D", Path.Combine(AppContext.BaseDirectory, "Aria2\\Config\\aria2.conf"));

private string RPCServerLink => "http://127.0.0.1:6300/jsonrpc";

Expand All @@ -29,8 +27,7 @@ public class Aria2Service : IAria2Service
/// </summary>
public async Task InitializeAria2Async()
{
await Aria2ProcessHelper.RunCmdAsync();
await Aria2ProcessHelper.ExecuteCmdAsync(Aria2ExecuteCmd);
await Aria2ProcessHelper.RunAria2Async(Aria2FileName, Aria2Arguments);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion GetStoreAppConsole/GetStoreAppConsole.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
10 changes: 8 additions & 2 deletions GetStoreAppPackage/GetStoreAppPackage.wapproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PropertyGroup>
<ProjectGuid>ca6c2dc4-c96d-4c23-ae18-fc7150bef554</ProjectGuid>
<TargetPlatformVersion>10.0.22621.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<TargetPlatformMinVersion>10.0.18362.0</TargetPlatformMinVersion>
<DefaultLanguage>en-US</DefaultLanguage>
<AppxPackageSigningEnabled>True</AppxPackageSigningEnabled>
<NoWarn>$(NoWarn);NU1702</NoWarn>
Expand Down Expand Up @@ -64,7 +64,13 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GetStoreAppConsole\GetStoreAppConsole.csproj" />
<ProjectReference Include="..\GetStoreApp\GetStoreApp.csproj" />
<ProjectReference Include="..\GetStoreApp\GetStoreApp.csproj">
<SkipGetTargetFrameworkProperties>True</SkipGetTargetFrameworkProperties>
<PublishProfile>Properties\PublishProfiles\win10-$(Platform).pubxml</PublishProfile>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.1" />
</ItemGroup>
<Import Project="$(WapProjPath)\Microsoft.DesktopBridge.targets" />
<Target Name="GetStoreAppProjectStompSourceProjectForWapProject" BeforeTargets="_ConvertItems">
Expand Down
4 changes: 2 additions & 2 deletions GetStoreAppPackage/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
</Properties>

<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.22621.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.22621.0" />
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.18362.0" MaxVersionTested="10.0.22621.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.18362.0" MaxVersionTested="10.0.22621.0" />
</Dependencies>

<Resources>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

### 下载与安装注意事项

> * 该程序使用的是Windows 应用 SDK构建的,建议您的系统版本为Windows 11(代号 21H2 / 内部版本号 22000)或更高版本,最低版本为Windows 10(代号1709 / 内部版本号17763)或更高版本。
> * 该程序使用的是Windows 应用 SDK构建的,建议您的系统版本为Windows 11(代号 21H2 / 内部版本号 22000)或更高版本,最低版本为Windows 10(代号1803 / 内部版本号18362)或更高版本。
> * 如果您的系统是Windows 10,应用功能存在一些限制:
暂不支持设置云母/亚克力背景色
应用部分图标使用的是Segoe Fluent Icons图标,这一类型图标并没有内置到Windows图标,所以初次打开应用时会存在图标异常的问题。需要您亲自下载相应的[图标文件](https://docs.microsoft.com/zh-cn/windows/apps/design/downloads/#fonts),点击右键菜单安装该字体图标文件,重启应用图标才能正常显示。
Expand Down

0 comments on commit 9e0f50b

Please sign in to comment.