文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C#实现只许一个实例运行(使用mutex类)

C#实现只许一个实例运行(使用mutex类)

时间:2010-09-30  来源:白开水易拉罐

在google上搜了很多(全是中文的),找了半天也没有解决问题。

最后没办法,只能搜索e版的了。于是找到了下面的答案。

metex是mutual exclusion 的缩写,

完整的实现参照一下:

代码
using System.Runtime.InteropServices;
namespace TST.SINGLE
{
public class TSTPGM
{
[DllImport(
"user32.dll")]
[
return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(
false);
Application.Run(
new MainForm());
}
else
{
Process current
= Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
}
}
}
}
}

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载