Program.cs
4.0 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using URSoldering.Common;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace URSoldering.Client
{
static class Program
{
#region 只启动一个实例
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll ", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
try
{
Process currentproc = Process.GetCurrentProcess();
Process[] processcollection = Process.GetProcessesByName(currentproc.ProcessName.Replace(".vshost", string.Empty));
// 该程序已经运行,
bool isShow = false;
if (processcollection.Length >= 1)
{
//MessageBox.Show("processcollection.Length >= 1");
foreach (Process process in processcollection)
{
if (process.Id != currentproc.Id)
{
// 如果进程的句柄为0,即代表没有找到该窗体,即该窗体隐藏的情况时
if (process.MainWindowHandle.ToInt32().Equals(0))
{
// 获得窗体句柄
string title = ConfigAppSettings.GetValue(Setting_Init.App_Title);
IntPtr formhwnd = FindWindow(null, title);
// 重新显示该窗体并切换到带入到前台
int SW_RESTORE = 9;
ShowWindow(formhwnd, SW_RESTORE);
SwitchToThisWindow(formhwnd, true);
isShow = true;
}
else
{
// 如果窗体没有隐藏,就直接切换到该窗体并带入到前台
// 因为窗体除了隐藏到托盘,还可以最小化
SwitchToThisWindow(process.MainWindowHandle, true);
isShow = true;
}
}
}
}
if (!isShow)
{
XmlConfigurator.Configure();
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMenu());
}
}
catch (Exception ex)
{
LogUtil.error(ex.ToString());
}
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
LogUnhandledException(e.ExceptionObject);
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
LogUnhandledException(e.Exception);
}
static void LogUnhandledException(object exceptionobj)
{
//这里可以进一步地写日志
LogUtil.error(exceptionobj.ToString());
}
}
}