PDA

View Full Version : لود شدن برنامه فقط برای یه بار!



arash1718
سه شنبه 10 مهر 1386, 11:14 صبح
برای اینکه وقتی برنامه در حال اجراست دیگر اجرا نشود چی کار کنم؟ شما از ریجیستری استفاده می کنید؟

Mahdi.Kiani
سه شنبه 10 مهر 1386, 11:31 صبح
روشهای مختلفی است برای براورده کردن این هدف
چک کردن پروسس های ویندوز
رجیستری
Mutex
و ...

خودم از Mutex استفاده میکنم
هم کد نویسی زیادی نداره و هم کار را را میندازه
البته استفاده از چک کردن پروسس های ویندوز مزایای دیگه ای نسبت به Mutex داره ولی خوب Mutex کارت را را میندازه

این کدها را در در کلاس progrm و در تابع Main و قبل از کد مربوط به Run کردن برنامه یعنی Application.Run قرار بده




System.Threading.Mutex mymutex = new System.Threading.Mutex(false, "myApppp");
if (!mymutex.WaitOne(100, false))
{
MessageBox.Show("one Instance of program is already running");
return;

}



البته دستور MessaegBox را گذاشتم که نمونه اجرا را ببینی
پس میتونی اونو برداری و کد را به صورت زیر بنویسی



System.Threading.Mutex mymutex = new ystem.Threading.Mutex(false, "myApppp");
if (!mymutex.WaitOne(100, false))
return;


پس تابع main برنامت یه چیزی شبیه به این باید باشه




System.Threading.Mutex mymutex = new ystem.Threading.Mutex(false, "myApppp");
if (!mymutex.WaitOne(100, false))
return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new frmMain());



که البته اگه System.threading را هم همون بالای کلاس اضافه کنی کیتونی مستقیما از mutex استفاده کنی (البته این موضوع را قطعا میدونستی محض اطلاع گفنم)

موفق باشید

arash1718
سه شنبه 10 مهر 1386, 18:51 عصر
روشهای مختلفی است برای براورده کردن این هدف
چک کردن پروسس های ویندوز
رجیستری
Mutex
و ...

خودم از Mutex استفاده میکنم
هم کد نویسی زیادی نداره و هم کار را را میندازه
البته استفاده از چک کردن پروسس های ویندوز مزایای دیگه ای نسبت به Mutex داره ولی خوب Mutex کارت را را میندازه

این کدها را در در کلاس progrm و در تابع Main و قبل از کد مربوط به Run کردن برنامه یعنی Application.Run قرار بده




System.Threading.Mutex mymutex = new System.Threading.Mutex(false, "myApppp");
if (!mymutex.WaitOne(100, false))
{
MessageBox.Show("one Instance of program is already running");
return;

}

البته دستور MessaegBox را گذاشتم که نمونه اجرا را ببینی
پس میتونی اونو برداری و کد را به صورت زیر بنویسی



System.Threading.Mutex mymutex = new ystem.Threading.Mutex(false, "myApppp");
if (!mymutex.WaitOne(100, false))
return;
پس تابع main برنامت یه چیزی شبیه به این باید باشه




System.Threading.Mutex mymutex = new ystem.Threading.Mutex(false, "myApppp");
if (!mymutex.WaitOne(100, false))
return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new frmMain());

که البته اگه System.threading را هم همون بالای کلاس اضافه کنی کیتونی مستقیما از mutex استفاده کنی (البته این موضوع را قطعا میدونستی محض اطلاع گفنم)

موفق باشید


میشه اون دو روش رو هم بگید؟

Mahdi.Kiani
سه شنبه 10 مهر 1386, 20:32 عصر
میشه اون دو روش رو هم بگید؟

نه نمیشه
دلیل ==>> جستجو (همین سایت + google (http://www.google.com) + wrox (http://www.p2p.wrox.com) + ....)

hassan razavi
سه شنبه 10 مهر 1386, 23:15 عصر
اینم یگ روش دیگه از طریق Process :


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

public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
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);
}

[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;
}

Mahdi.Kiani
چهارشنبه 11 مهر 1386, 11:49 صبح
اینم یگ روش دیگه از طریق Process :


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

public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
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);
}

[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;
}


لینک اصلی فراموش نشه (http://barnamenevis.org/forum/showthread.php?t=6202&highlight=HandleRunningInstance)
کد فوق را کاربر Mahdavi گذاشتن
گفته بودم توی سایت جستجو کنید (پست 4 همین تاپیک)

موفق باشید