如题。还有如何判断程序是否已经运行,只运行一个应用程序?
解决方案 »
- datalist 与gridview 输出到EXCEL 的问题(急、望高手指教)
- 假如C++写的里面有内存泄露,会对C#调用这个DLL的时候造成怎样的影响
- 下面这段代码的的具体含义。请懂的人加一下注释。。
- 求救!一个SQL语句,在线急等
- 用DataTable承载数据消耗大不大?比起用对象数组承载数据集消耗大吗?
- 发票打印的问题,预览是一张实际却出两张
- 如何解析下面格式的XML?
- 有没有人用过Sniffer这个软件,用C#能做出这种软件吗?
- 老大们 ! 快进来 !!!帮不上忙也来捧个场啊 !!!!!!
- 关于.net,送分问题,今天结贴。
- 数据邦定后显示不出来????晕!!!!请高手进来LOOK LOOK
- C#如何执行多条dos指令
是说在窗体的构造函数中定义一个静态变量和一个私有的构造函数?不好意思,本人是个新手对设计模式这方面有点薄弱。请各位指点。
如果用Singleton 模式,在FormUserInfo中应该做何设置?
点击时又要怎么做?
关于如何判断程序已运行,只运行一个应用程序,使用下述语句:
static void Main()
{
System.Diagnostics.Process[] ProcessList = System.Diagnostics.Process.GetProcessesByName("RemotingTest");//填应用程序名
if (ProcessList.Length > 1)
{
MessageBox.Show("已有相同软件在运行!");
}
else
Application.Run(new Form1());
}
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection; public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
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;
}
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL); //Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
} [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;
}
×××××××××××××××××××××××××××××××××××××××
static void Main()
{
//判断之前是否已经打开了相同的进程
//new Bestzone.MIS.Report.F1BookContainer();
bool alreadyExist = false;
try
{
System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process p in ps)
{
if(p.ProcessName.Equals(curP.ProcessName) && p.Id != curP.Id)
{
alreadyExist = true;
}
}
}
catch(System.PlatformNotSupportedException ex){ex.ToString();}
catch(System.InvalidOperationException ex){ex.ToString();}//如果已经存在,则放弃本进程
if(alreadyExist)
{
return;
}Application.Run(new MainForm());
}
public Form1()
{
SingleMutex = new Mutex(false, "SINGLE_INSTANCE_MUTEX");
if (!SingleMutex.WaitOne(0,false))
{
SingleMutex.Close();
SingleMutex=null;
return ;
}
.....}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (this.SingleMutex !=null)
{
this.SingleMutex.ReleaseMutex();
this.SingleMutex.Close();
this.SingleMutex=null;
}
.....
}
}
具体使用参见.net sdk :
ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfSystemThreadingMutexMembersTopic.htm
{
if (IsSingleProcess ())
{
Application.Run(new Form1 ());
}
}
// 是否只有一个进程
private static bool IsSingleProcess ()
{
int i = System.Diagnostics.Process.GetProcessesByName (System.Diagnostics.Process.GetCurrentProcess ().ProcessName).Length ;
if (i == 1)
return true;
else
return false ; }
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new frm());
else
MessageBox.Show("应用程序已启动!");
即不让实例化frmMain,只是提供一个方法来返回frmMain的一个静态实例
这样就保证了所有的child的父亲是同一个
当实例化子窗口的时候,循环frmMain所有的打开子窗口,如果已经打开了一个,那么就
不用再打开该子窗口了