Files
ClientServerProject/软件系统客户端模版/Program.cs

82 lines
2.7 KiB
C#
Raw Normal View History

using ClientsLibrary;
using HslCommunication.LogNet;
using System;
2017-02-22 14:30:10 +08:00
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
2017-02-22 14:30:10 +08:00
namespace
{
static class Program
{
/// <summary>
/// 指示了应用程序退出时的代码
/// </summary>
public static int QuitCode { get; set; } = 0;
2017-02-22 14:30:10 +08:00
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
// 处理异常
UserClient.LogNet = new LogNetDateTime(Application.StartupPath + @"\Logs", GenerateMode.ByEveryDay);
// 捕获未处理的异常
AppDomain.CurrentDomain.UnhandledException += ClientsLibrary.UserClient.CurrentDomain_UnhandledException;
2017-02-22 14:30:10 +08:00
//=====================================================================
// 为了强制只启动一个应用程序的实例
2017-02-22 14:30:10 +08:00
Process process = Process.GetCurrentProcess();
// 遍历应用程序的同名进程组
2017-02-22 14:30:10 +08:00
foreach (Process p in Process.GetProcessesByName(process.ProcessName))
{
// 不是同一进程则关闭刚刚创建的进程
2017-02-22 14:30:10 +08:00
if (p.Id != process.Id)
{
// 此处显示原先的窗口需要一定的时间,不然无法显示
2017-02-22 14:30:10 +08:00
ShowWindowAsync(p.MainWindowHandle, 9);
SetForegroundWindow(p.MainWindowHandle);
System.Threading.Thread.Sleep(10);
Application.Exit();// 关闭当前的应用程序
2017-02-22 14:30:10 +08:00
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//===================================================================
// 运行主窗口之前先进行账户的验证
P1:
2017-02-22 14:30:10 +08:00
FormLogin login = new FormLogin();
if (login.ShowDialog() == DialogResult.OK)
{
login.Dispose();
FormMainWindow fmw = new FormMainWindow();
Application.Run(fmw);
if (QuitCode == 1)
{
// 继续显示登录窗口
2017-04-05 15:16:34 +08:00
QuitCode = 0;
goto P1;
}
2017-02-22 14:30:10 +08:00
}
else
{
login.Dispose();
}
}
2017-02-22 14:30:10 +08:00
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}
}