最近看了一个老外的写的方法,通过进程间通信保证只启动一个程序:
客户端主窗体继承该接口,
![](/Images/OutliningIndicators/ContractedBlock.gif)
SingleInstancing
using System;
![](/Images/OutliningIndicators/None.gif)
namespace SingleInstancing
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Provides methods which a single instance application can use to in order to be respond to any new instance of it.
/// </summary>
public interface ISingleInstanceEnforcer
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Handles messages received from a new instance of the application.
/// </summary>
/// <param name="e">The EventArgs object holding information about the event.</param>
void OnMessageReceived(MessageEventArgs e);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Handles a creation of a new instance of the application.
/// </summary>
/// <param name="e">The EventArgs object holding information about the event.</param>
void OnNewInstanceCreated(EventArgs e);
}
}
注意,由于该方法可能是在另一个进程中调用所以在其中调用主窗体时需要回到主窗体
![](/Images/OutliningIndicators/ContractedBlock.gif)
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SingleInstancing;
using System.Runtime.InteropServices;
using Common;
![](/Images/OutliningIndicators/None.gif)
namespace WindowsApplication1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class Form1 : Form, ISingleInstanceEnforcer
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private int i =1;
![](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
}
![](/Images/OutliningIndicators/InBlock.gif)
private void Form1_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetLabel();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
ISingleInstanceEnforcer 成员#region ISingleInstanceEnforcer 成员
![](/Images/OutliningIndicators/InBlock.gif)
public void OnMessageReceived(MessageEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](/Images/OutliningIndicators/InBlock.gif)
public void OnNewInstanceCreated(EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i++;
SetLabel();
}
![](/Images/OutliningIndicators/InBlock.gif)
private void SetLabel()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.Invoke(new MethodInvoker(delegate()
{
this.label1.Text = i.ToString();
//ShowWindowAsync(this.Handle, 1);
WindowUtility.SetActiveWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
WindowUtility.ShowWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle, WindowUtility.SW_RESTORE);
WindowUtility.SetForegroundWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
}));
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
namespace WindowsApplication1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
partial class Form1
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disposing && (components != null))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
base.Dispose(disposing);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(127, 109);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(11, 12);
this.label1.TabIndex = 0;
this.label1.Text = "1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Label label1;
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
启动程序如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Text;
using SingleInstancing;
using System.Windows.Forms;
![](/Images/OutliningIndicators/None.gif)
namespace WindowsApplication1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
[Serializable]
public class App
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Singleton#region Singleton
![](/Images/OutliningIndicators/InBlock.gif)
private static App instance = new App();
![](/Images/OutliningIndicators/InBlock.gif)
public static App Instance
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return instance;
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Fields#region Fields
![](/Images/OutliningIndicators/InBlock.gif)
private SingleInstanceTracker tracker = null;
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
public Form MainForm
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (Form1)tracker.Enforcer;
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Method#region Method
private ISingleInstanceEnforcer GetSingleInstanceEnforcer()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new Form1();
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
#endregion
public void Start(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Attempt to create a tracker
tracker = new SingleInstanceTracker("SingleInstanceSample", new SingleInstanceEnforcerRetriever(GetSingleInstanceEnforcer));
![](/Images/OutliningIndicators/InBlock.gif)
// If this is the first instance of the application, run the main form
if (tracker.IsFirstInstance)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Form1 form = (Form1)tracker.Enforcer;
Application.Run(form);
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Dispose();
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// This is not the first instance of the application, so do nothing but send a message to the first instance
if (args.Length > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tracker.SendMessageToFirstInstance(args);
}
}
}
catch (SingleInstancingException ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("Could not create a SingleInstanceTracker object:\n" + ex.Message + "\nApplication will now terminate.\n" + ex.InnerException.ToString());
![](/Images/OutliningIndicators/InBlock.gif)
return;
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tracker != null)
tracker.Dispose();
}
}
}
}
![](/Images/OutliningIndicators/None.gif)
具体代码:
Winform单一实例