C#winforms如何禁止同一个程序运行两次?
解决方案 »
- 多对数(a,b),(x,y),(a,b)等我用什么来存储根据第一个数能方便的查找到第二个数?
- 如何避免出现“未将对象引用设置到对象的实例”?
- C# UserControl
- C# winform 设置功能的窗口怎么设计(左边是设置项,右边是groupbox)
- form1 点一个button 让form1隐藏 让form2显示出来 怎么写啊 高手求教
- ###200分求助###Session页面出现"数据无法验证"的错误
- datagrid编辑问题
- 打印的问题,急~~!
- 请问c#中查询数据库后,如果库中无满足条件的纪录,这样的代码如何些。delphi中是 query.record<=0.
- 在.net中进行存储过程调试时,为何不出现黄色小箭头
- 多项删除CheckListBox问题?
- C#绘图闪烁问题。
bool canCreateNew;
m_ServerMutex = new Mutex(true, "EMTASS_SERVER", out canCreateNew);
if (!canCreateNew)
{
throw new Exception("Can create two or more server!");
}
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
if ("进程名" == myProcess.ProcessName)
// 处理办法
}
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
System.Diagnostics.Process _RunProcess = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] _Process = System.Diagnostics.Process.GetProcessesByName(_RunProcess.ProcessName); if (_Process.Length == 1)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
//重复提示
}
}
[STAThread]
public static void Main()
{
bool ret;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
if (ret)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行实现 XP 可视风格
System.Windows.Forms.Application.DoEvents();
// Main 为你程序的主窗体,如果是控制台程序不用这句
System.Windows.Forms.Application.Run(new Main());
mutex.ReleaseMutex();
}
else
{
MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// 提示信息,可以删除。
Application.Exit();//退出程序
}
}
Process[] myProcesses;
myProcesses = Process.GetProcessesByName("NotePad.exe");
if (myProcesses.Length == 0)
{
p = Process.Start"NotePad.exe");
}
错误 只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句
"mutex.ReleaseMutex; // 这个地方报错;"
我在5楼给你的代码中哪里用到“mutex.ReleaseMutex”?
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Configuration;
using System.IO; string name = Process.GetCurrentProcess().ProcessName;
int id = Process.GetCurrentProcess().Id;
Process[] prc = Process.GetProcesses();
foreach (Process pr in prc)
{
if ((name == pr.ProcessName) && (pr.Id != id))
{
MessageBox.Show("对不起,本地已经有该系统正在运行!\n ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
System.Environment.Exit(0);
}
}
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行实现 XP 可视风格
System.Windows.Forms.Application.DoEvents();
// Main 为你程序的主窗体,如果是控制台程序不用这句
System.Windows.Forms.Application.Run(new Main());
mutex.ReleaseMutex();
}
else
{
MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// 提示信息,可以删除。
Application.Exit();//退出程序
}
呵呵,不好意思,没看仔细是用到的。
但没有问题啊,给你个全部代码看看吧using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bool ret;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
if (ret)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
mutex.ReleaseMutex();
}
else
{
MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// 提示信息,可以删除。
Application.Exit();//退出程序
}
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;namespace FallingGold
{
static class Program
{
private static Mutex mutex;
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mutex = new Mutex(true, "一个");
//保证只有一个实例运行
if (mutex.WaitOne(0, false))
{
Application.Run(new Form1());
}
}
}
}