PDA

View Full Version : فقط يكبار اجرا شدن برنامه Check Instance Of App



debugger
چهارشنبه 26 فروردین 1388, 20:17 عصر
دوستان لطفا كد Check Instance Of App را براي يكبار اجرا شدن برنامه را قرار دهيد . هر كار كردم نتونستم بنويسم

unique1984
چهارشنبه 26 فروردین 1388, 20:32 عصر
سلام
کدهای زیر را باید در کلاس StartUp برنامه اعمال کرد:

using System.Diagnostics;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (IsPrevInstance())
return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
private static bool IsPrevInstance()
{
string processName = Process.GetCurrentProcess().ProcessName;
Process[] instances = Process.GetProcessesByName(processName);
if (instances.Length > 1)
return true;
else
return false;
}
}


وقتی که برنامه اجرا می شه در اولین مرحله تابع ()IsPrevInstance لیست پردازشهای CPU رو بررسی می کنه، اگه برنامه در حال اجرا بود، بلافاصله پردازش اون رو متوقف می کنه تا کاربر با نسخه در حال اجرا به کار خود ادامه بده.

prankster
پنج شنبه 27 فروردین 1388, 09:11 صبح
بهتر است بعد از ایجاد اولین instance از برنامه process های ایجاد شده بعدی را از بین ببریم و instance اصلی را فعال کنیم، با این کار با هر بار اجرای جدید برنامه برنامه اصلی به پنجره فعال تبدیل خواهد شد:



static class Program
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

///<summary>
/// The main entry point for the application.
///</summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

//use Mutex to prevent multi instances of the program
System.Threading.Mutex mutex = null;
bool owned = false;

try
{
mutex = new System.Threading.Mutex(true, Assembly.GetExecutingAssembly().FullName, out owned);
if (!owned)
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}

Process.GetCurrentProcess().Kill();
}
}
catch (Exception ex)
{
}
finally
{
if (mutex != null && owned)
mutex.ReleaseMutex();
}

Application.Run(new Form1());
}
}

debugger
پنج شنبه 27 فروردین 1388, 10:12 صبح
جناب وقتي كد شما را اعمل مي كنم اين ارور را ميدهد

Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) D:\source\PasswordKeeper\PasswordKeeper\Program.cs 11 10 PasswordKeeper

prankster
پنج شنبه 27 فروردین 1388, 10:59 صبح
namespace های زیر را در برنامه use کنید:



using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;

NewFoxStudent
پنج شنبه 27 فروردین 1388, 13:44 عصر
چرا از Nutex استفاده نمیکنید
در این صورت دیگه نیازی به گرفتن تمام Proccess ها نیست و خیلی سریع تره

prankster
پنج شنبه 27 فروردین 1388, 14:00 عصر
برای تست run بودن application از Mutex استفاده شد، process فقط برای active کردن application جاری است