实现单实例的问题? 急急!!!!
loginForm frm = new loginForm();
//保证单实例运行
bool newMutexCreated = false;
string mutexName = "Local\\" +
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
Mutex mutex = null;
try
{
// Create a new mutex object with a unique name
mutex = new Mutex(false , mutexName, out newMutexCreated);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace +
// "\n\n" + "Application Exiting...", "Exception thrown");
Application.Exit();
}
// When the mutex is created for the first time
// we run the program since it is the first instance.
if (newMutexCreated)
{
//判断是否打开主窗口
if (DialogResult.OK == frm.ShowDialog())
{
Application.Run(new MsnManage());
}
}
写了一个单实例程序,开始还可以实现,过一关时间不知道为什么不好用拉,有没有高手指点一下迷津
[解决办法]
用下面的代码可以支持单例
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace SingleInstance
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//这里需要更改你的程序的Process名称
if (!IsExist("SingleInstance"))
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("Exist one Instance");
Application.Exit();
}
}
private static bool IsExist(string name)
{
//获取系统进程
Process[] prces = Process.GetProcesses();
int count = 0;
foreach (Process pro in prces)
{
if(pro.ProcessName==name)
{
count++;
}
}
if(count>1)
return true;
else
return false;
}
}
}
[解决办法]
using System.Diagnostics;//用于使程序只运行一次
using System.Reflection;//用于使程序只运行一次
using System.Runtime.InteropServices; //用于使程序只运行一次
using System.IO;//用于使程序只运行一次
[STAThread]
static void Main()
{
//用于使程序只运行一次
Process instance=RunningInstance();
if( instance!= null )
{
//MessageBox.Show( "程序已在运行!" );
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle , 1);
//设置真实例程为foreground window
SetForegroundWindow (instance.MainWindowHandle);
return;
}
.........
........
/// <summary>
/// 确保窗口没有被最小化或最大化 (用于使程序只运行一次)
/// </summary>
/// <param name="hWnd">进程中主窗口的窗口句柄</param>
/// <param name="cmdShow">1还原窗口,2 窗口最小化,3 窗口最大化</param>
/// <returns></returns>
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow);
/// <summary>
/// 设置真实例程为foreground window (用于使程序只运行一次)
/// </summary>
/// <param name="hWnd">进程中主窗口的窗口句柄</param>
/// <returns></returns>
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 使程序只运行一次
/// 本函数用于判断当前程序是否有另一个实例在运行
/// </summary>
/// <returns></returns>
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
//在判断进程模块文件名是否相等这部分的代码,是可选的。
//如果当前的程序在文件系统中只存在一个的话,以上的方法是可以的;
//否则不要删除这部分的代码。
//这种方法,速度比较慢,其次通过ProcessName去系统中查寻,
//有可能查出来的Process并不是我想要得,虽说在后面加了文件目录判断,
//但是其含有潜在的问题(前面已经说出来)。
}
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if (e.Button==MouseButtons.Left )
{
if(hti.Type == DataGrid.HitTestType.Cell)
{
//dataGrid1.Select(hti.Row);
dataGrid1.CurrentRowIndex=hti.Row;
if (hti.Column==0)
{
BindingManagerBase bm = dataGrid1.BindingContext[dataGrid1.DataSource,dataGrid1.DataMember];
DataRow datarow =((System.Data.DataRowView)bm.Current).Row;
if (datarow!=null)
{
if((bool)datarow["se"])
datarow["se"]=false;
else
datarow["se"]=true;
}
}
}
}
}