Skip to content

Commit 1b5d4a7

Browse files
committed
防止启动多个实例
1 parent df4377c commit 1b5d4a7

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

TimeControl/Program.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using System.Windows.Forms;
6+
using System.Diagnostics;
67

78
namespace TimeControl
89
{
@@ -22,6 +23,24 @@ static void Main(string[] args)
2223
hide = true;
2324
}
2425
}
26+
Process[] processes = Process.GetProcessesByName("TimeControl");
27+
if (processes.Length > 1)
28+
{
29+
if (MessageBox.Show(
30+
"当前已经启动TimeControl,不能启动多个实例,是否要重新启动?",
31+
"提示",
32+
MessageBoxButtons.OKCancel,
33+
MessageBoxIcon.Information) == DialogResult.OK)
34+
{
35+
foreach (Process process in processes)
36+
{
37+
if (process.Id != Environment.ProcessId)
38+
process.Kill();
39+
}
40+
}
41+
else
42+
return;
43+
}
2544
Application.SetHighDpiMode(HighDpiMode.SystemAware);
2645
Application.EnableVisualStyles();
2746
Application.SetCompatibleTextRenderingDefault(false);

TimeControl/TimeControl.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<PlatformTarget>AnyCPU</PlatformTarget>
1414
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
1515
<Platforms>AnyCPU;x64;x86</Platforms>
16+
<GenerateDocumentationFile>False</GenerateDocumentationFile>
1617
</PropertyGroup>
1718

1819
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
@@ -58,8 +59,4 @@
5859
</None>
5960
</ItemGroup>
6061

61-
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
62-
<Exec Command="cmd /c killtc.bat" />
63-
</Target>
64-
6562
</Project>

TimeControl/killtc.bat

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)