简单的只运行一个程序实例[原创]
1
using System;
2
using System.Collections.Generic;
3
using System.Windows.Forms;
4
using BOM.NewUI;
5
using System.Threading;
6![](/Images/OutliningIndicators/None.gif)
7
namespace NewUI
8
{
9
static class Program
10
{
11
/// <summary>
12
/// 应用程序的主入口点。
13
/// </summary>
14
[STAThread]
15
static void Main()
16
{
17
try
18
{
19
//声明互斥体。
20
Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
21
//判断互斥体是否使用中。
22
bool running = !mutex.WaitOne(0, false);
23
if (!running)
24
{
25
log4net.Config.XmlConfigurator.Configure();
26
Application.EnableVisualStyles();
27
Application.SetCompatibleTextRenderingDefault(false);
28
Thread.CurrentThread.Name = "GUI Main Thread";
29
Application.Run(new MainContext());
30
}
31
}
32
catch (Exception ex)
33
{
34
MessageBox.Show(ex.ToString());
35
log4net.LogManager.GetLogger("Main Thread").Error("error:", ex);
36![](/Images/OutliningIndicators/InBlock.gif)
37
}
38
}
39![](/Images/OutliningIndicators/InBlock.gif)
40![](/Images/OutliningIndicators/InBlock.gif)
41
}
42
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)