C#winform程序 如何避免开两个

有时想起 2017-01-06 02:21:49
如果一个程序最小化了 用户没注意到 又去点开重开了一个 这就会出错 如何避免
...全文
594 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_42038506 2019-12-02
  • 打赏
  • 举报
回复
bool createNew;
// createdNew:
// 在此方法返回时,如果创建了局部互斥体(即,如果 name 为 null 或空字符串)或指定的命名系统互斥体,则包含布尔值 true;
// 如果指定的命名系统互斥体已存在,则为false
using (Mutex mutex = new Mutex(true, Application.ProductName, out createNew))
{
if (createNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
// 程序已经运行的情况,则弹出消息提示并终止此次运行
else
{
MessageBox.Show(Process.GetCurrentProcess().ProcessName + "程序已经在运行!", "退出程序", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Threading.Thread.Sleep(1000);

// 终止此进程并为基础操作系统提供指定的退出代码。
System.Environment.Exit(1);
}
}
nikolaichow 2017-01-13
  • 打赏
  • 举报
回复

bool isNew = false;
System.Threading.Mutex mInstance = new System.Threading.Mutex(true, Application.ProductName, out isNew);
 if (!isNew)
                {
                    MessageBox.Show("程序已经运行!");
                    return;
                }
飞天凤凰601 2017-01-10
  • 打赏
  • 举报
回复
在program.cs上如下操作: 添加引用 using System.Reflection; using System.Runtime.InteropServices; using System.Diagnostics; 调用DLL文件 [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 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); } 在MAIN中添加如下代码 public static void Main() { Process instance = RunningInstance(); if (instance == null) { Application.Run(new Form1()); } else { HandleRunningInstance(instance); } }
周二不加班 2017-01-10
  • 打赏
  • 举报
回复
给程序初始化的部分加判断,if(未初始化){初始化}
t_kong 2017-01-06
  • 打赏
  • 举报
回复
在方案的Program.cs中加入下面例子即可
static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        [STAThread]
        static void Main()
        {
			bool isAppRunning = false;
			Mutex mutex = new Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out isAppRunning);
			if (!isAppRunning) {
				MessageBox.Show("Program Is Running, Can't Open Again !!");
				Environment.Exit(1);
			}

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormMain());
        }
    }
crystal_lz 2017-01-06
  • 打赏
  • 举报
回复
Mutex。。。。。。。。。。。。。。。
love春 2017-01-06
  • 打赏
  • 举报
回复
Form f = Application.OpenForms["TakeNo"]; //查找是否打开过Form1窗体 if (f == null) //没打开过 { TakeNo fa = new TakeNo(); fa.Show(); //重新new一个Show出来 } else { f.Focus(); //打开过就让其获得焦点 }
  • 打赏
  • 举报
回复
http://www.cnblogs.com/justForMe/archive/2011/03/01/1967786.html
mjp1234airen4385 2017-01-06
  • 打赏
  • 举报
回复
利用windows的互斥方法,启动时检测同样的进程名。
绿领巾童鞋 2017-01-06
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/380252869 老问题了
  • 打赏
  • 举报
回复
在Main函数中加判断,进程存在不再次启动程序
Hertz_liu 2017-01-06
  • 打赏
  • 举报
回复
启动的时候检查进程是否存在

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧