c#中让Windows窗体只运行一次,并在第二次启动窗体时激活该窗体
不是MDI窗体,就一个单独的窗体就像迅雷一样迅雷在启动的时候点击关闭按钮就最小化到电脑的右下角了,然后在启动迅雷,却没有从新启动,而是把已经启动的迅雷激活了...请问用C...
不是MDI窗体,就一个单独的窗体
就像迅雷一样 迅雷在启动的时候点击关闭按钮就最小化到电脑的右下角了,然后在启动迅雷,却没有从新启动,而是把已经启动的迅雷激活了...请问用C#怎么实现。 展开
就像迅雷一样 迅雷在启动的时候点击关闭按钮就最小化到电脑的右下角了,然后在启动迅雷,却没有从新启动,而是把已经启动的迅雷激活了...请问用C#怎么实现。 展开
2个回答
展开全部
方法一:
/// <summary>
/// 从这里开始运行
/// </summary>
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
//没有实例在运行
WeatherApp appInstance = new WeatherApp();
appInstance.StartMainGui();
}
else
{
//已经有一个实例在运行
HandleRunningInstance(instance);
}
}
#region 确保只有一个实例
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
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;
}
}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("该应用系统已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
#endregion
方法二:
[STAThread]
static void Main(string[] args)
{
bool isFirst;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, "WindowAppTest", out isFirst);
//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突
if (!isFirst)
{
MessageBox.Show("Exist");
Environment.Exit(1);//实例已经存在,退出程序
}
else
{
Application.Run(new Form1());
}
}
方法三
把AssemblyInfo.cs里的[assembly: AssemblyFileVersion("1.0.0.0")]改为[assembly:AssemblyFileVersion("2.0.0.8")],然后利用该信息进行判断。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Collections;
using System.Threading;
namespace MyWork_01
{
class Program
{
static void Main(string[] args)
{
Process[] processes = Process.GetProcesses(); //获得当前所有进程
Process currentProcess = Process.GetCurrentProcess(); //获取当前正在运行进程
ProcessModule currentPM = currentProcess.Modules[0];
int same = 0; //相同运行实例个数
ArrayList proList = new ArrayList(); //将相同实例加入此集合中
foreach (Process p in processes)
{
try//由于进程不同,有的进程不包含Modules信息,所以要用try保护
{
if (p.Modules != null)
if (p.Modules.Count > 0)
{
System.Diagnostics.ProcessModule pm = p.Modules[0];
if (pm.FileVersionInfo.FileVersion.Equals(currentPM.FileVersionInfo.FileVersion))
{
same++;
proList.Add(p);
}
if (same > 1)
{
same++;
proList.Add(p);
if (same > 1)
{
for (int i = 0; i < proList.Count; i++)
{
if (((Process)(proList[i])).Id == currentProcess.Id)
{
Console.WriteLine("该进程已经启动了一个实例");
Thread.Sleep(1000);
((Process)(proList[i])).Kill();
}
}
}
}
}
}
catch
{ }
}
Console.Read();
}
}
}
方法四:直接定义一个属性类,利用此属性信息进行判断。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Threading;
[assembly: Help("This Assembly demonstrates custom attributes creation and their run-time query.")]
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
}
protected String description;
public String Description
{
get
{
return this.description;
}
}
}
class Program
{
static void Main(string[] args)
{
HelpAttribute HelpAttr1 = null;
HelpAttribute HelpAttr2 = null;
Process currentProcess = Process.GetCurrentProcess(); //获取当前正在运行进程
Assembly a = Assembly.LoadFrom(currentProcess.MainModule.FileName);
foreach (Attribute attr in a.GetCustomAttributes(true))
{
HelpAttr1 = attr as HelpAttribute;
if (null != HelpAttr1)
{
//Console.WriteLine("Description of {0}:\n{1}", currentProcess.MainModule.FileName, HelpAttr1.Description);
break;
}
}
Process[] processes = Process.GetProcesses(); //获得当前所有进程
int same = 0; //相同运行实例个数
ArrayList proList = new ArrayList(); //将相同实例加入此集合中
foreach (Process pro in processes)
{
try//由于进程不同,有的进程不包含Modules信息,所以要用try保护
{
if (pro.Modules != null)
if (pro.Modules.Count > 0)
{
Assembly b = Assembly.LoadFrom(pro.MainModule.FileName);
foreach (Attribute attr in b.GetCustomAttributes(true))
{
HelpAttr2 = attr as HelpAttribute;
if (null != HelpAttr2)
{
if (HelpAttr1.Description.Equals(HelpAttr2.Description))
{
same++;
proList.Add(pro);
if (same > 1)
{
for (int i = 0; i < proList.Count; i++)
{
if (((Process)(proList[i])).Id == currentProcess.Id )
{
Console.WriteLine("该进程已经启动了一个实例");
Thread.Sleep(1000);
((Process)(proList[i])).Kill();
}
}
}
}
}
}
}
}
catch
{
}
}
Console.ReadLine();
}
}
/// <summary>
/// 从这里开始运行
/// </summary>
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
//没有实例在运行
WeatherApp appInstance = new WeatherApp();
appInstance.StartMainGui();
}
else
{
//已经有一个实例在运行
HandleRunningInstance(instance);
}
}
#region 确保只有一个实例
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
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;
}
}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("该应用系统已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
#endregion
方法二:
[STAThread]
static void Main(string[] args)
{
bool isFirst;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, "WindowAppTest", out isFirst);
//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突
if (!isFirst)
{
MessageBox.Show("Exist");
Environment.Exit(1);//实例已经存在,退出程序
}
else
{
Application.Run(new Form1());
}
}
方法三
把AssemblyInfo.cs里的[assembly: AssemblyFileVersion("1.0.0.0")]改为[assembly:AssemblyFileVersion("2.0.0.8")],然后利用该信息进行判断。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Collections;
using System.Threading;
namespace MyWork_01
{
class Program
{
static void Main(string[] args)
{
Process[] processes = Process.GetProcesses(); //获得当前所有进程
Process currentProcess = Process.GetCurrentProcess(); //获取当前正在运行进程
ProcessModule currentPM = currentProcess.Modules[0];
int same = 0; //相同运行实例个数
ArrayList proList = new ArrayList(); //将相同实例加入此集合中
foreach (Process p in processes)
{
try//由于进程不同,有的进程不包含Modules信息,所以要用try保护
{
if (p.Modules != null)
if (p.Modules.Count > 0)
{
System.Diagnostics.ProcessModule pm = p.Modules[0];
if (pm.FileVersionInfo.FileVersion.Equals(currentPM.FileVersionInfo.FileVersion))
{
same++;
proList.Add(p);
}
if (same > 1)
{
same++;
proList.Add(p);
if (same > 1)
{
for (int i = 0; i < proList.Count; i++)
{
if (((Process)(proList[i])).Id == currentProcess.Id)
{
Console.WriteLine("该进程已经启动了一个实例");
Thread.Sleep(1000);
((Process)(proList[i])).Kill();
}
}
}
}
}
}
catch
{ }
}
Console.Read();
}
}
}
方法四:直接定义一个属性类,利用此属性信息进行判断。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Threading;
[assembly: Help("This Assembly demonstrates custom attributes creation and their run-time query.")]
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
}
protected String description;
public String Description
{
get
{
return this.description;
}
}
}
class Program
{
static void Main(string[] args)
{
HelpAttribute HelpAttr1 = null;
HelpAttribute HelpAttr2 = null;
Process currentProcess = Process.GetCurrentProcess(); //获取当前正在运行进程
Assembly a = Assembly.LoadFrom(currentProcess.MainModule.FileName);
foreach (Attribute attr in a.GetCustomAttributes(true))
{
HelpAttr1 = attr as HelpAttribute;
if (null != HelpAttr1)
{
//Console.WriteLine("Description of {0}:\n{1}", currentProcess.MainModule.FileName, HelpAttr1.Description);
break;
}
}
Process[] processes = Process.GetProcesses(); //获得当前所有进程
int same = 0; //相同运行实例个数
ArrayList proList = new ArrayList(); //将相同实例加入此集合中
foreach (Process pro in processes)
{
try//由于进程不同,有的进程不包含Modules信息,所以要用try保护
{
if (pro.Modules != null)
if (pro.Modules.Count > 0)
{
Assembly b = Assembly.LoadFrom(pro.MainModule.FileName);
foreach (Attribute attr in b.GetCustomAttributes(true))
{
HelpAttr2 = attr as HelpAttribute;
if (null != HelpAttr2)
{
if (HelpAttr1.Description.Equals(HelpAttr2.Description))
{
same++;
proList.Add(pro);
if (same > 1)
{
for (int i = 0; i < proList.Count; i++)
{
if (((Process)(proList[i])).Id == currentProcess.Id )
{
Console.WriteLine("该进程已经启动了一个实例");
Thread.Sleep(1000);
((Process)(proList[i])).Kill();
}
}
}
}
}
}
}
}
catch
{
}
}
Console.ReadLine();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
点下面有个 激活MDI就好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询