如何使程序只运行一个实例 做了个播放器,安装了以后,双击文件,打开一个窗口。双击另一个文件,则又打开一个心得窗口。如何实现只运行一个实例,而且如果已经有窗口在运行,怎么将双击播放的文件名传给这个已存在的窗口? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1. 用Mutex锁 http://blog.csdn.net/fangxinggood/archive/2010/10/28/5972908.aspx#A22. 需要做文件关联,这样会把点击的文件路径传给应用 应用用 Environment.GetCommandLineArgs(); 取 bool bExist; Mutex MyMutex=new Mutex(true,"OnlyRunOncetime",out bExist); if(bExist) { Application.Run(new Form1()); MyMutex.ReleaseMutex(); } else { MessageBox.Show("程序已经运行!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } 创建单例winform应用程序的一种更好的方式 http://blog.csdn.net/xuexiaodong2009/archive/2011/05/04/6393859.aspx /// <summary> /// 检测程序是否已经运行 /// </summary> /// <returns>bool true:未运行 false:已经运行</returns> static bool IsRun() { Process[] prs = Process.GetProcesses(); Process thisone = Process.GetCurrentProcess(); foreach (Process pr in prs) { if (thisone.ProcessName == pr.ProcessName && thisone.Id != pr.Id) { return false; } } return true; } 一、using System;using System.Runtime.InteropServices;using System.Windows.Forms;using System.Diagnostics;using System.Reflection;public class OneInstnace{ //Api声明,并声明一 [DllImport("User32.dll")] private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1; [STAThread] public static void Main() { //得到正在运行的例程 Process instance = RunningInstance(); if (instance == null) { //如果没有其它例程,就新建一个窗体 Application.Run (new OneInstnace()); } else { //处理发现的例程 HandleRunningInstance(instance); } } public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName); //遍历正在有相同名字运行的例程 foreach (Process process in processes) { //忽略现有的例程 if (process.Id != current.Id) { //确保例程从EXE文件运行 if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==current.MainModule.FileName) { //返回另一个例程实例 return process; } } } //没有其它的例程,返回Null return null; } public static void HandleRunningInstance(Process instance) { //确保窗口没有被最小化或最大化 ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL); //设置真实例程为foreground window SetForegroundWindow (instance.MainWindowHandle); }}二、view sourceprint?/// <summary>/// 单实例运行/// </summary>/// <returns> true 应用程序已启动,false 则没有 </returns>public bool SingleRun(ref System.Threading.Mutex mutex ){ mutex = new System.Threading.Mutex(false, "WINDOWS"); if (!mutex.WaitOne(0, false)) { mutex.Close(); mutex = null; } if (mutex == null) { return true; } return false;}using System;using System.Threading;using System.Windows.Forms;static class Program{ public static EventWaitHandle ProgramStarted; [STAThread] static void Main() { // 尝试创建一个命名事件 bool createNew; ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew); // 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出 if (!createNew) { ProgramStarted.Set(); return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }}C# code// Form1.cs//using System;using System.Windows.Forms;using System.Threading;public partial class Form1 : Form{ NotifyIcon notifyIcon1 = new NotifyIcon(); public Form1() { //InitializeComponent(); this.notifyIcon1.Text = "Double click me to show window"; this.notifyIcon1.Icon = System.Drawing.SystemIcons.Information; 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) { this.notifyIcon1.ShowBalloonTip(2000, "Hello", "I am here...", ToolTipIcon.Info); }} // 三、using System;using System.Threading;using System.Windows.Forms;static class Program{ public static EventWaitHandle ProgramStarted; [STAThread] static void Main() { // 尝试创建一个命名事件 bool createNew; ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew); // 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出 if (!createNew) { ProgramStarted.Set(); return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }}C# code// Form1.cs//using System;using System.Windows.Forms;using System.Threading;public partial class Form1 : Form{ NotifyIcon notifyIcon1 = new NotifyIcon(); public Form1() { //InitializeComponent(); this.notifyIcon1.Text = "Double click me to show window"; this.notifyIcon1.Icon = System.Drawing.SystemIcons.Information; 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) { this.notifyIcon1.ShowBalloonTip(2000, "Hello", "I am here...", ToolTipIcon.Info); }} static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { bool initiallyOwned = true; bool isCreated; //Mutex m = new Mutex( initiallyOwned, "MyTest", out isCreated); Mutex m = new Mutex( initiallyOwned, Application.ProductName, out isCreated); if ( !(initiallyOwned && isCreated) ) { MessageBox.Show( "抱歉,程序只能在一台机上运行一个实例!", "提示" ); Application.Exit(); } else { Application.Run(new Zfirst()); }}} WINFORM的combox如何绑定默认值? 很奇怪的sql语句吖! select top 0 from 表名 求助为将对象引用设置到对象实例 急!如何取得系统桌面鼠标的单,双击事件! aspxgridview 行双击事件怎么写的? 如何把Winform中的窗体角做成圆的 C#WINFROM下如何设置pictureBox的背景真正透明 dll全局钩子 这样的值怎样不同窗体传递? 谁能发个magiclibrary给我,收到就给分 C#实现语音聊天! 在线等....dataset 转 xml
http://blog.csdn.net/fangxinggood/archive/2010/10/28/5972908.aspx#A2
2. 需要做文件关联,这样会把点击的文件路径传给应用
应用用 Environment.GetCommandLineArgs(); 取
Mutex MyMutex=new Mutex(true,"OnlyRunOncetime",out bExist);
if(bExist)
{
Application.Run(new Form1());
MyMutex.ReleaseMutex();
}
else
{
MessageBox.Show("程序已经运行!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
http://blog.csdn.net/xuexiaodong2009/archive/2011/05/04/6393859.aspx
/// 检测程序是否已经运行
/// </summary>
/// <returns>bool true:未运行 false:已经运行</returns>
static bool IsRun()
{
Process[] prs = Process.GetProcesses();
Process thisone = Process.GetCurrentProcess();
foreach (Process pr in prs)
{
if (thisone.ProcessName == pr.ProcessName && thisone.Id != pr.Id)
{
return false;
}
}
return true;
}
一、
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
public class OneInstnace
{
//Api声明,并声明一
[DllImport("User32.dll")] private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1; [STAThread] public static void Main()
{
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run (new OneInstnace());
}
else
{
//处理发现的例程
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==current.MainModule.FileName)
{
//返回另一个例程实例
return process;
}
}
}
//没有其它的例程,返回Null
return null;
}
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
}
二、
view sourceprint?
/// <summary>
/// 单实例运行
/// </summary>
/// <returns> true 应用程序已启动,false 则没有 </returns>
public bool SingleRun(ref System.Threading.Mutex mutex )
{
mutex = new System.Threading.Mutex(false, "WINDOWS");
if (!mutex.WaitOne(0, false))
{
mutex.Close();
mutex = null;
}
if (mutex == null)
{
return true;
}
return false;
}
using System;
using System.Threading;
using System.Windows.Forms;static class Program
{
public static EventWaitHandle ProgramStarted; [STAThread]
static void Main()
{
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew); // 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出
if (!createNew)
{
ProgramStarted.Set();
return;
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}C# code// Form1.cs
//
using System;
using System.Windows.Forms;
using System.Threading;public partial class Form1 : Form
{
NotifyIcon notifyIcon1 = new NotifyIcon(); public Form1()
{
//InitializeComponent();
this.notifyIcon1.Text = "Double click me to show window";
this.notifyIcon1.Icon = System.Drawing.SystemIcons.Information;
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)
{
this.notifyIcon1.ShowBalloonTip(2000, "Hello", "I am here...", ToolTipIcon.Info);
}
}
// 三、
using System;
using System.Threading;
using System.Windows.Forms;static class Program
{
public static EventWaitHandle ProgramStarted; [STAThread]
static void Main()
{
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew); // 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出
if (!createNew)
{
ProgramStarted.Set();
return;
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}C# code// Form1.cs
//
using System;
using System.Windows.Forms;
using System.Threading;public partial class Form1 : Form
{
NotifyIcon notifyIcon1 = new NotifyIcon(); public Form1()
{
//InitializeComponent();
this.notifyIcon1.Text = "Double click me to show window";
this.notifyIcon1.Icon = System.Drawing.SystemIcons.Information;
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)
{
this.notifyIcon1.ShowBalloonTip(2000, "Hello", "I am here...", ToolTipIcon.Info);
}
}
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{ bool initiallyOwned = true;
bool isCreated;
//Mutex m = new Mutex( initiallyOwned, "MyTest", out isCreated); Mutex m = new Mutex( initiallyOwned, Application.ProductName, out isCreated);
if ( !(initiallyOwned && isCreated) )
{
MessageBox.Show( "抱歉,程序只能在一台机上运行一个实例!", "提示" );
Application.Exit();
}
else
{
Application.Run(new Zfirst());
}
}
}