-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
61 lines (56 loc) · 1.88 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Windows.Forms;
using System.Threading;
using System.Net.NetworkInformation;
using System.Net;
namespace PrintApp
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bool createNew;
using (System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName, out createNew))
{
if (createNew)
{
if (PortInUse(8088))
{
MessageBox.Show("系统8088端口被占用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
HttpServer httpServer = new WebServer(8088);
Thread thread = new Thread(new ThreadStart(httpServer.listen));
thread.Start();
Application.Run(new Form1());
}
}
else
{
MessageBox.Show("打印面单程序已经启动","提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
static bool PortInUse(int port)
{
bool inUse = false;
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
foreach (IPEndPoint endPoint in ipEndPoints)
{
if (endPoint.Port == port)
{
inUse = true;
break;
}
}
return inUse;
}
}
}