Skip to content

Commit 563ae2c

Browse files
committed
Improve: Better the Win32 api
1 parent e5f08d2 commit 563ae2c

File tree

4 files changed

+53
-32
lines changed

4 files changed

+53
-32
lines changed

Avalonia.CpuLimiter/Models/AdminRunner.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Runtime.InteropServices;
56
using System.Security.Principal;
67
using System.Text;
78
using System.Threading.Tasks;
@@ -12,10 +13,17 @@ public class AdminRunner
1213
{
1314
public static bool IsRunAsAdmin()
1415
{
15-
WindowsIdentity identity = WindowsIdentity.GetCurrent();
16-
WindowsPrincipal principal = new(identity);
16+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
17+
{
18+
WindowsIdentity identity = WindowsIdentity.GetCurrent();
19+
WindowsPrincipal principal = new(identity);
1720

18-
return principal.IsInRole(WindowsBuiltInRole.Administrator);
21+
return principal.IsInRole(WindowsBuiltInRole.Administrator);
22+
}
23+
else
24+
{
25+
throw new PlatformNotSupportedException();
26+
}
1927
}
2028

2129
public static void RunElevated()
@@ -38,15 +46,20 @@ public static void RunElevated()
3846
}
3947
}
4048

41-
public static void RunAsAdmin(int CPUCoreNum, string Path)
49+
public static void RunAsAdmin(int cpuCoreNum, string path)
4250
{
4351

44-
if (string.IsNullOrWhiteSpace(Path))
52+
if (string.IsNullOrWhiteSpace(path))
4553
throw new InvalidOperationException("The executable file Path cannot be empty");
4654

47-
Console.WriteLine("Running as adminisitrator.");
55+
if(IsRunAsAdmin())
56+
Console.WriteLine("Running as adminisitrator.");
57+
else
58+
{
59+
Console.WriteLine($"Running without administrator privileges.");
60+
}
4861

49-
ProcessStartInfo startInfo = new ProcessStartInfo(Path)
62+
ProcessStartInfo startInfo = new ProcessStartInfo(path)
5063
{
5164
UseShellExecute = false,
5265
RedirectStandardInput = true,
@@ -62,28 +75,18 @@ public static void RunAsAdmin(int CPUCoreNum, string Path)
6275

6376

6477
process.Start();
65-
Win32CpuAffinity win32CpuAffinity = new(CPUCoreNum);
78+
Win32CpuAffinity win32CpuAffinity = new(cpuCoreNum);
6679

67-
process = win32CpuAffinity.SetProcessWithLimitedCPU(process);
80+
process = win32CpuAffinity.SetProcessWithLimitedCpu(process);
6881

69-
if (process != null)
70-
{
71-
Console.WriteLine($"Process started with PID: {process.Id}");
72-
73-
process.WaitForExit();
74-
75-
int exitCode = process.ExitCode;
76-
Console.WriteLine($"Process exited with exit code: {exitCode}");
77-
78-
process.Close();
82+
Console.WriteLine($"Process started with PID: {process.Id}");
7983

80-
}
81-
else
82-
{
83-
Console.WriteLine("Failed to start process.");
84+
process.WaitForExit();
8485

85-
}
86+
int exitCode = process.ExitCode;
87+
Console.WriteLine($"Process exited with exit code: {exitCode}");
8688

89+
process.Close();
8790
}
8891
}
8992
}

Avalonia.CpuLimiter/Models/NativeCPUAffinity.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using System.Diagnostics;
7+
using System.Runtime.InteropServices;
78

89
namespace Avalonia.CpuLimiter.Models
910
{
10-
class NativeCPUAffinity
11+
class NativeCpuAffinity
1112
{
1213
// 写一个利用c#原生 api 限制当前程序调用CPU核心的类
1314

@@ -17,7 +18,12 @@ public static void SetProcessAffinity(int processId, int cpuId)
1718
Process process = System.Diagnostics.Process.GetCurrentProcess();
1819

1920
// 设置进程的CPU亲和性
20-
process.ProcessorAffinity = 1 << cpuId;
21+
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
22+
process.ProcessorAffinity = 1 << cpuId;
23+
else
24+
{
25+
throw new PlatformNotSupportedException("not supported on this platform");
26+
}
2127

2228
}
2329

@@ -26,7 +32,13 @@ public static void SetProcessAffinity(int cpuId)
2632
// 获取当前进程
2733
Process process = System.Diagnostics.Process.GetCurrentProcess();
2834
// 设置进程的CPU亲和性
29-
process.ProcessorAffinity = 1 << cpuId;
35+
36+
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
37+
process.ProcessorAffinity = 1 << cpuId;
38+
else
39+
{
40+
throw new PlatformNotSupportedException("not supported on this platform");
41+
}
3042

3143
}
3244

@@ -36,7 +48,13 @@ public static int GetProcessAffinity(int processId)
3648
System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
3749

3850
// 获取进程的CPU亲和性
39-
return process.ProcessorAffinity.ToInt32();
51+
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
52+
return process.ProcessorAffinity.ToInt32();
53+
else
54+
{
55+
throw new PlatformNotSupportedException("not supported on this platform");
56+
}
57+
4058
}
4159

4260

Avalonia.CpuLimiter/Models/Win32CpuAffinity.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace Avalonia.CpuLimiter.Models;
88

99
class Win32CpuAffinity
1010
{
11-
public Win32CpuAffinity(int CPULogicalCoreNum)
11+
public Win32CpuAffinity(int cpuLogicalCoreNum)
1212
{
13-
CpuLogicalCoreNum = CPULogicalCoreNum;
13+
CpuLogicalCoreNum = cpuLogicalCoreNum;
1414
}
1515

1616
[DllImport("kernel32.dll", SetLastError = true)]
@@ -19,7 +19,7 @@ public Win32CpuAffinity(int CPULogicalCoreNum)
1919
[DllImport("kernel32.dll", SetLastError = true)]
2020
public static extern bool GetProcessAffinityMask(nint hProcess, nint dwProcessAffinityMask, nint lpSystemAffinityMask);
2121

22-
public Process SetProcessWithLimitedCPU(Process process)
22+
public Process SetProcessWithLimitedCpu(Process process)
2323
{
2424
if (CpuLogicalCoreNum <= 0)
2525
{

Avalonia.CpuLimiter/Services/FilesService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public FilesService(Window target)
2424
{
2525
var files = await _target.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
2626
{
27-
Title = "Open Text File",
27+
Title = "Select executable file",
2828
AllowMultiple = false
2929

3030

0 commit comments

Comments
 (0)