Files
ClientServerProject/软件系统服务端模版/Program.cs
2017-06-16 16:22:12 +08:00

47 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process process = Process.GetCurrentProcess();
//遍历应用程序的同名进程组
foreach (Process p in Process.GetProcessesByName(process.ProcessName))
{
//不是同一进程则关闭刚刚创建的进程
if (p.Id != process.Id)
{
//此处显示原先的窗口需要一定的时间,不然无法显示
ShowWindowAsync(p.MainWindowHandle, 9);
SetForegroundWindow(p.MainWindowHandle);
System.Threading.Thread.Sleep(10);
Application.Exit();//关闭当前的应用程序
return;
}
}
//设置应用程序的线程池数量防止服务器端卡死状态根据内存及CPU进行更改
System.Threading.ThreadPool.SetMaxThreads(1000, 256);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormServerWindow());
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}
}