客户端主窗体继承该接口,
SingleInstancing
using System;
namespace SingleInstancing
{
/**//// <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
{
/**//// <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);
/**//// <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);
}
}
using System;
namespace SingleInstancing
{
/**//// <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
{
/**//// <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);
/**//// <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);
}
}
注意,由于该方法可能是在另一个进程中调用所以在其中调用主窗体时需要回到主窗体
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;
namespace WindowsApplication1
{
public partial class Form1 : Form, ISingleInstanceEnforcer
{
private int i =1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetLabel();
}
ISingleInstanceEnforcer 成员#region ISingleInstanceEnforcer 成员
public void OnMessageReceived(MessageEventArgs e)
{
}
public void OnNewInstanceCreated(EventArgs e)
{
i++;
SetLabel();
}
private void SetLabel()
{
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);
}));
}
#endregion
}
}
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;
namespace WindowsApplication1
{
public partial class Form1 : Form, ISingleInstanceEnforcer
{
private int i =1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetLabel();
}
ISingleInstanceEnforcer 成员#region ISingleInstanceEnforcer 成员
public void OnMessageReceived(MessageEventArgs e)
{
}
public void OnNewInstanceCreated(EventArgs e)
{
i++;
SetLabel();
}
private void SetLabel()
{
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);
}));
}
#endregion
}
}
namespace
WindowsApplication1
{
partial class Form1
{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
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();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
{
partial class Form1
{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
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();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
启动程序如下:
Code
using System;
using System.Collections.Generic;
using System.Text;
using SingleInstancing;
using System.Windows.Forms;
namespace WindowsApplication1
{
[Serializable]
public class App
{
Singleton#region Singleton
private static App instance = new App();
public static App Instance
{
get
{
return instance;
}
}
#endregion
Fields#region Fields
private SingleInstanceTracker tracker = null;
#endregion
Properties#region Properties
public Form MainForm
{
get
{
return (Form1)tracker.Enforcer;
}
}
#endregion
Method#region Method
private ISingleInstanceEnforcer GetSingleInstanceEnforcer()
{
return new Form1();
}
public void Dispose()
{
}
#endregion
public void Start(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
// Attempt to create a tracker
tracker = new SingleInstanceTracker("SingleInstanceSample", new SingleInstanceEnforcerRetriever(GetSingleInstanceEnforcer));
// If this is the first instance of the application, run the main form
if (tracker.IsFirstInstance)
{
try
{
Form1 form = (Form1)tracker.Enforcer;
Application.Run(form);
}
finally
{
Dispose();
}
}
else
{
// This is not the first instance of the application, so do nothing but send a message to the first instance
if (args.Length > 0)
{
tracker.SendMessageToFirstInstance(args);
}
}
}
catch (SingleInstancingException ex)
{
MessageBox.Show("Could not create a SingleInstanceTracker object:\n" + ex.Message + "\nApplication will now terminate.\n" + ex.InnerException.ToString());
return;
}
finally
{
if (tracker != null)
tracker.Dispose();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using SingleInstancing;
using System.Windows.Forms;
namespace WindowsApplication1
{
[Serializable]
public class App
{
Singleton#region Singleton
private static App instance = new App();
public static App Instance
{
get
{
return instance;
}
}
#endregion
Fields#region Fields
private SingleInstanceTracker tracker = null;
#endregion
Properties#region Properties
public Form MainForm
{
get
{
return (Form1)tracker.Enforcer;
}
}
#endregion
Method#region Method
private ISingleInstanceEnforcer GetSingleInstanceEnforcer()
{
return new Form1();
}
public void Dispose()
{
}
#endregion
public void Start(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
// Attempt to create a tracker
tracker = new SingleInstanceTracker("SingleInstanceSample", new SingleInstanceEnforcerRetriever(GetSingleInstanceEnforcer));
// If this is the first instance of the application, run the main form
if (tracker.IsFirstInstance)
{
try
{
Form1 form = (Form1)tracker.Enforcer;
Application.Run(form);
}
finally
{
Dispose();
}
}
else
{
// This is not the first instance of the application, so do nothing but send a message to the first instance
if (args.Length > 0)
{
tracker.SendMessageToFirstInstance(args);
}
}
}
catch (SingleInstancingException ex)
{
MessageBox.Show("Could not create a SingleInstanceTracker object:\n" + ex.Message + "\nApplication will now terminate.\n" + ex.InnerException.ToString());
return;
}
finally
{
if (tracker != null)
tracker.Dispose();
}
}
}
}
具体代码:
Winform单一实例