static void Main()
{
// MessageBox.Show("数据库连接成功!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
FrmLogin FrmLog = new FrmLogin();
FrmLog.ShowDialog();
if (FrmLogin.CanLogin == true)
{
bool creatnew;
System.Threading.Mutex m = new System.Threading.Mutex(false, "ysucs", out creatnew);
if (creatnew)
{
Application.EnableVisualStyles();
Application.Run(new FrmMdiMain());
m.ReleaseMutex();
}
else
{
MessageBox.Show("不能同时运行两个实例~!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
return;
}
}感觉代码没什么错误啊,但是还是能运行两个实例,郁闷。
{
// MessageBox.Show("数据库连接成功!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
FrmLogin FrmLog = new FrmLogin();
FrmLog.ShowDialog();
if (FrmLogin.CanLogin == true)
{
bool creatnew;
System.Threading.Mutex m = new System.Threading.Mutex(false, "ysucs", out creatnew);
if (creatnew)
{
Application.EnableVisualStyles();
Application.Run(new FrmMdiMain());
m.ReleaseMutex();
}
else
{
MessageBox.Show("不能同时运行两个实例~!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
return;
}
}感觉代码没什么错误啊,但是还是能运行两个实例,郁闷。
解决方案 »
- Winform 软件问题
- C#.net 画树形组织结构图
- 创建二维数组
- 水晶报表动态改变纸张时,总是出现以下错误“设置打印机发生错误,错误信息为:无效索引。 (异常来自 HRESULT:0x8002000B (DISP_E_BADINDEX))”
- 怎样调用dll文件呀?
- 哪位高手用c#写过com组件并成功用delphi7调用成功过的????
- 请问如何使在c#中使系统宽带自动拔号?
- 求助!在含有DockPanel的窗体中,当弹出MsgBox后,窗体所有事件失效
- 写一个文件上传的web控件(急)用c#
- 资源文件里面自己定义的图片、文字都在哪里放?
- tree和dataview怎么用,大家帮帮忙
- [C#环境问题]:VS2003如何打开用VS2005编写的工程?
不过我的Main方法里没有登录窗体的创建,所以总是可能出现在这上面,互斥类的用法和你写的一样
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (runone())
Application.Run(new Form4
());
return 1;
}
private static System.Threading.Mutex mutex;
private static bool runone()
{
bool one;
mutex = new System.Threading.Mutex(true, "WindowsApplication2", out one);
return one;
}
}
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (runone())
Application.Run(new Form4
());
return 1;
}
private static System.Threading.Mutex mutex;
//静态...的才可以。
private static bool runone()
{
bool one;
mutex = new System.Threading.Mutex(true, "WindowsApplication2", out one);
return one;
}
}
[STAThread]
static void Main(string[] args)
{
if (!IsRun())
{
System.Windows.Forms.Application.Run(new frm1());
}
else
{
System.Windows.Forms.MessageBox.Show("程序已经运行");
}
}
private bool IsRun()
{
int i=0;
Process[] ps=Process.GetProcesses();
foreach(Process p in ps)
{
if (p.ProcessName.ToLower()=="自身程序名称(显示在系统任务列表中名称)")
i++;
}
return i<2; //包括自身
}
/// <summary>
/// Start 的摘要说明。
/// </summary>
public class Start
{
[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; public Start()
{
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
if(!GetRunningInstance(true))
{
Application.Run(new frmMdi());
}
}
public static bool GetRunningInstance(bool AutoActive)
{
Process currentProcess = Process.GetCurrentProcess(); //获取当前进程
//获取当前运行程序完全限定名
string currentFileName = currentProcess.MainModule.FileName;
//获取进程名为ProcessName的Process数组。
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
//遍历有相同进程名称正在运行的进程
bool r=false;
foreach (Process process in processes)
{
if (process.MainModule.FileName == currentFileName)
{
if (process.Id != currentProcess.Id) //根据进程ID排除当前进程
{
r=true;
if(AutoActive)
HandleRunningInstance( process);//返回已运行的进程实例
}
}
}
return r;
}
public static bool GetRunningInstance()
{
return GetRunningInstance(false);
}
public static bool HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置为foreground window
return SetForegroundWindow(instance.MainWindowHandle);
}
}