ably ably برای هدایت شدن به سایت جدید اینجا کلیک کنید

آموزش برنامه نویسی

آموزش برنامه نویسی وب، اندروید، ویندوز , اسکیوال و...
Tell: 0912 097 5516
| Email: Hello@EduOnline.ir

۱ مطلب با کلمه‌ی کلیدی «How to open one instance of application» ثبت شده است

جلوگیری از باز شدن چندین نسخه از نرم افزار

بسم الله الرحمن الرحیم

در بعضی نرم افزار ها شما می بایست اجازه اجرا کردن چندین نسخه از نرم افزار را از کاربر سلب نمایید.

برای اجرا کردن تنها یک نسخه از نرم افزار توسط کاربر می توان به صورت زیر عمل کنید:

فایل program.cs را باز کنید و در آن کد زیر را اضافه کنید

public static Process PriorProcess()
    // Returns a System.Diagnostics.Process pointing to
    // a pre-existing process with the same name as the
    // current one, if any; or null if the current process
    // is unique.
    {
        Process curr = Process.GetCurrentProcess();
        Process[] procs = Process.GetProcessesByName(curr.ProcessName);
        foreach (Process p in procs)
        {
            if ((p.Id != curr.Id) &&
                (p.MainModule.FileName == curr.MainModule.FileName))
                return p;
        }
        return null;
    }

حال متد Main را به صورت زیر عوض کنید:

[STAThread]
    static void Main()
    {
        if (PriorProcess() != null)
        {

            MessageBox.Show("Another instance of the app is already running.");
            return;
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form());
    }

up
ما را در گوگل محبوب کنید