1.初始化项目。保险起见bin目录完整提交 2.基于现场代码为调整过的初始项目(此次提交还未解决代码差异问题)
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
|
||||
namespace AGVSystem
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
[DllImport("User32.dll")]
|
||||
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
||||
[DllImport("User32.dll")]
|
||||
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
|
||||
private const int WS_SHOWNORMAL = 3;
|
||||
/// <summary>
|
||||
/// 应用程序的主入口点。
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
try
|
||||
{
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Process instance = RunningInstance();
|
||||
mainfrm.init2();
|
||||
if (instance == null)
|
||||
{
|
||||
Application.Run(new mainfrm());
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleRunningInstance(instance);
|
||||
}
|
||||
}
|
||||
catch(Exception ex) { }
|
||||
//bool runOne;
|
||||
//Mutex run = new Mutex(true, "AGVSystem", out runOne);
|
||||
//if (runOne)
|
||||
//{
|
||||
// run.ReleaseMutex();
|
||||
// Application.EnableVisualStyles();
|
||||
// Application.SetCompatibleTextRenderingDefault(false);
|
||||
// Application.Run(new Form1());
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// MessageBox.Show("程序已运行!");
|
||||
//}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取正在运行的实例,没有运行的实例返回null;
|
||||
/// </summary>
|
||||
public static Process RunningInstance()
|
||||
{
|
||||
Process current = Process.GetCurrentProcess();
|
||||
Process[] processes = Process.GetProcessesByName(current.ProcessName);
|
||||
foreach (Process process in processes)
|
||||
{
|
||||
if (process.Id != current.Id)
|
||||
{
|
||||
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
|
||||
{
|
||||
return process;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示已运行的程序。
|
||||
/// </summary>
|
||||
public static void HandleRunningInstance(Process instance)
|
||||
{
|
||||
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //显示,可以注释掉
|
||||
SetForegroundWindow(instance.MainWindowHandle); //放到前端
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user