2010年11月13日星期六

c#让程序只运行一个实例vs2010

C# winform窗体实现托盘和恢复窗体及相关操作
程序只能运行一个实例。
如果程序已经存在,且最小化,则还原那个程序。
如果程序已经存在,且不是最小化(最大化或正常状态)

在Main里给找到的已经存在的主窗口发送自定义消息,主窗口接收并处理。
这个可以。
要小心的是,如果已经运行的窗口被隐藏(比如放到系统托盘上),那么它Process.MainWindowHandle返回的句柄可能为空。
拿不到它的句柄,你就不能向它发窗口消息。

如何做转换呢?或者还有其他方法?
可以用命名事件来通知,而对于数量不多的参数,可以通过注册表来传递,详见例子。
// Program.cs 文件内容
using System;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.Diagnostics;

namespace WindowsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew);

// 如果该命名事件已经存在(存在有前一个运行实例),则
if (!createNew)
{
// 先写一些数据到注册表中,以便传递给前一个运行实例
Registry.SetValue(@"HKEY_CURRENT_USER\Software\MyMy", "", DateTime.Now.ToLongTimeString());

// 发事件通知
ProgramStarted.Set();

// 等一小会以便前一个运行实例接到通知后恢复显示(也可以采用等待事件通知的方式)
Thread.Sleep(200);

// 将焦点转移到前一个实例
foreach (Process p in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
{
if (p.MainWindowHandle != IntPtr.Zero)
{
SetForegroundWindow(p.MainWindowHandle);
}
}

// 就此退出第二个进程
return;
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static EventWaitHandle ProgramStarted;

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
}
}
//Form1.cs 文件内容

using System;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
NotifyIcon notifyIcon1 = new NotifyIcon();

public Form1()
{
//InitializeComponent();
this.Controls.Add(new TextBox());

this.notifyIcon1.Text = "Double click me to show window";
this.notifyIcon1.Icon = System.Drawing.SystemIcons.Question;
this.notifyIcon1.DoubleClick += OnNotifyIconDoubleClicked;

this.SizeChanged += OnSizeChanged;
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
}

// 当最小化时,放到系统托盘。
void OnSizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Visible = false;
}
}

// 当双击托盘图标时,恢复窗口显示
void OnNotifyIconDoubleClicked(object sender, EventArgs e)
{
this.Visible = true;
this.notifyIcon1.Visible = false;
this.WindowState = FormWindowState.Normal;
}

// 当收到第二个进程的通知时,从注册表中获得传入的参数,并恢复窗口显示
void OnProgramStarted(object state, bool timeout)
{
if (this.InvokeRequired)
{
this.Invoke(new WaitOrTimerCallback(OnProgramStarted), state, timeout);
}
else
{
object param = Registry.GetValue(@"HKEY_CURRENT_USER\Software\MyMy", "", string.Empty);
this.Text = param as string;
OnNotifyIconDoubleClicked(this, EventArgs.Empty);
}
}
}
}