• ¡Welcome to Square Theme!
  • This news are in header template.
  • Please ignore this message.
مهمان عزیز خوش‌آمدید. ورود عضــویت


امتیاز موضوع:
  • 5 رای - 3.6 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
Title: اجرای فقط یک کپی از برنامه
حالت موضوعی
#1
این دستور چک می کند اگر هیچ برنامه ای از نوع برنامه فراخوانی شده در حال اجرا نباشد ، برنامه اجرا می شود.
کد:
using System;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Diagnostics;



namespace SingleInstanceTest

{

    static class Program

    {

        [System.Runtime.InteropServices.DllImport("user32.dll")]

        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]

        static extern bool SetForegroundWindow(IntPtr hWnd);



        public static bool HasPriorInstance()

        {

            Process currentProcess = Process.GetCurrentProcess();

            string fileName = currentProcess.StartInfo.FileName;

            foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))

            {

                if (process.Id == currentProcess.Id) { continue; }

                if (process.StartInfo.FileName != fileName) { continue; }

                SetForegroundWindow(process.MainWindowHandle);

                return true;

            }

            return false;

        }



        [STAThread]

        static void Main()

        {

            if (!HasPriorInstance())

            {

                Application.EnableVisualStyles();

                Application.SetCompatibleTextRenderingDefault(false);

                Application.Run(new Form1());

            }

        }

    }

}
 
پاسخ
  


موضوعات مشابه ...
موضوع نویسنده پاسخ بازدید آخرین ارسال
Bug پشتیبانی آموزشی رایگان برای فراگیران زبان های برنامه نویسی one hacker alone 0 2,685 08-09-2016، 11:53 PM
آخرین ارسال: one hacker alone
  نمونه برنامه با الگریتمA* mahdis 1 3,578 01-21-2013، 09:23 PM
آخرین ارسال: Amin_Mansouri
Sad اضافه کردن صوت و تصویر به برنامه چت milad.ahmadie 10 17,604 06-14-2012، 01:39 PM
آخرین ارسال: Amin_Mansouri
  برنامه انبار دار Pxt 0 3,310 05-01-2012، 01:43 PM
آخرین ارسال: Pxt
Information باز کردن سورس برنامه های C# analyze 3 10,720 01-01-2012، 05:19 PM
آخرین ارسال: Amin_Mansouri
  سورس کد بدست اورن لیست برنامه های نصب شده c# Amin_Mansouri 0 3,958 12-30-2011، 03:57 PM
آخرین ارسال: Amin_Mansouri
  اجرای ماشین حساب ویندوز در برنامه analyze 0 6,357 12-06-2011، 02:47 PM
آخرین ارسال: analyze
Photo برنامه ارسال بلوتوث analyze 2 5,509 12-06-2011، 02:29 PM
آخرین ارسال: analyze
  اتصال به sql sqrverدر برنامه های تحت شبکه Ghoghnus 3 10,538 12-05-2011، 11:53 PM
آخرین ارسال: analyze
  مسیر جاری برنامه Ghoghnus 0 5,194 08-02-2011، 03:44 PM
آخرین ارسال: Ghoghnus

پرش به انجمن:


Browsing: 1 مهمان