Skip to content

Commit cd08042

Browse files
committed
fix batchcoretest
1 parent 5e69945 commit cd08042

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

SCADA/Program/BatchCoreTest/Program.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using System.ServiceModel;
34

45
namespace BatchCoreService
@@ -11,6 +12,8 @@ static class Program
1112
static void Main()
1213
{
1314
BatchCoreTest srv = new BatchCoreTest();
15+
ConsoleUtil.RegisterCloseConsoleHandle();//注册控制台关闭事件,注意,只有执行该该操作,事件
16+
ConsoleUtil.ClosedConsole += (sender, e) => srv.Dispose();
1417
Console.ReadLine();
1518
}
1619
}
@@ -47,4 +50,130 @@ public void Dispose()
4750
}
4851
}
4952
}
53+
54+
public static class ConsoleUtil
55+
{
56+
#region 禁用控制台关闭按钮
57+
58+
///
59+
/// 禁用关闭按钮
60+
///
61+
public static void DisableCloseButton()
62+
{
63+
DisableCloseButton(Console.Title);
64+
}
65+
66+
///
67+
/// 禁用关闭按钮
68+
///
69+
/// 控制台名字
70+
public static void DisableCloseButton(string consoleName)
71+
{
72+
73+
IntPtr windowHandle = FindWindow(null, consoleName);
74+
IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);
75+
uint scClose = 0xF060;
76+
RemoveMenu(closeMenu, scClose, 0x0);
77+
}
78+
79+
#region API
80+
[DllImport("User32.dll", EntryPoint = "FindWindow")]
81+
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
82+
83+
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
84+
static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
85+
86+
[DllImport("user32.dll", EntryPoint = "RemoveMenu")]
87+
static extern IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
88+
89+
#endregion
90+
91+
#endregion
92+
93+
#region 捕捉控制台关闭事件
94+
95+
public delegate bool ConsoleCtrlDelegate(int ctrlType);
96+
97+
[DllImport("kernel32.dll")]
98+
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate handlerRoutine, bool add);
99+
100+
101+
102+
///
103+
/// 注册控制台关闭事件,通过事件进行捕捉.
104+
///
105+
public static void RegisterCloseConsoleHandle()
106+
{
107+
SetConsoleCtrlHandler(OnClosedConsole, true);
108+
}
109+
110+
///
111+
/// 当控制台被关闭时,引发事件.
112+
///
113+
public static event EventHandler ClosedConsole;
114+
115+
private static bool OnClosedConsole(int ctrlType)
116+
{
117+
if (ClosedConsole != null)
118+
{
119+
var e = new CloseConsoleEventArgs((CloseConsoleCategory)ctrlType);
120+
ClosedConsole("Console", e);
121+
return e.IsCancel;
122+
}
123+
return false; //忽略处理,让系统进行默认操作
124+
}
125+
126+
#endregion
127+
128+
}
129+
130+
///
131+
/// 控制台关闭事件.
132+
///
133+
public class CloseConsoleEventArgs : EventArgs
134+
{
135+
public CloseConsoleEventArgs()
136+
{
137+
138+
}
139+
140+
public CloseConsoleEventArgs(CloseConsoleCategory category)
141+
{
142+
Category = category;
143+
}
144+
145+
public CloseConsoleCategory Category { get; set; }
146+
147+
///
148+
/// 是否取消操作.
149+
///
150+
public bool IsCancel { get; set; }
151+
}
152+
153+
///
154+
/// 关闭控制台的类型.
155+
///
156+
public enum CloseConsoleCategory
157+
{
158+
///
159+
/// 当用户关闭Console
160+
///
161+
CloseEvent = 2,
162+
///
163+
/// Ctrl+C
164+
///
165+
CtrlCEvent = 0,
166+
///
167+
/// 用户退出(注销)
168+
///
169+
LogoffEvent = 5,
170+
///
171+
/// Ctrl+break
172+
///
173+
CtrlBreakEvent = 1,
174+
///
175+
/// 系统关闭
176+
///
177+
ShutdownEvent = 6,
178+
}
50179
}

SCADA/dll/OPCDriver.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)