جلوگیری از باز شدن مجدد برنامه
solution in Windows form application Prohibit again run application(reopen application). ۱- first add Class RunAlready.cs ۲-Call method processIsRunning() with Name Process from RunAlready.cs in Program.cs Program.cs: ===================
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace Tirage.MainStand { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { PublicClass.Class.RunAlready RunAPP = new PublicClass.Class.RunAlready(); string outApp = RunAPP.processIsRunning("Tirage.MainStand"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MainStand_FrmLogin fLogin = new MainStand_FrmLogin(); if (outApp.Length == 0) { if (fLogin.ShowDialog() == DialogResult.OK) { Application.Run(new MainStand_masterFrm()); } } else MessageBox.Show( "Instance already running"); } } } |
class RunAlready: ===================
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PublicClass.Class { public class RunAlready { public string processIsRunning(string process) { string xdescription = ""; System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(process); foreach (System.Diagnostics.Process proc in processes) { var iddd = System.Diagnostics.Process.GetCurrentProcess().Id; if (proc.Id != System.Diagnostics.Process.GetCurrentProcess().Id) { xdescription = "Application Run At time:" + proc.StartTime.ToString() + System.Environment.NewLine; xdescription += "Current physical memory : " + proc.WorkingSet64.ToString() + System.Environment.NewLine; xdescription += "Total processor time : " + proc.TotalProcessorTime.ToString() + System.Environment.NewLine; xdescription += "Virtual memory size : " + proc.VirtualMemorySize64.ToString() + System.Environment.NewLine; } } return xdescription; } } } |
منبع...
آخرین دیدگاهها