PDA

View Full Version : اجرای فقط یک کپی از برنامه



omidamiry
شنبه 22 مهر 1391, 19:31 عصر
این کد دارای خطاس کی بلده رفع کنه

using System;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Diagnostics;



namespace SingleInstanceTest

{

static class Program

{

[System.Runtime.InteropServices.DllImport(user32.dl l)]

[return System.Runtime.InteropServices.MarshalAs(System.Ru ntime.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.ProcessN ame))

{

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(fals e);

Application.Run(new Form1());

}

}

}

}

این دو خط رو نمشناسه

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

static extern bool SetForegroundWindow(IntPtr hWnd);

danialafshari
شنبه 22 مهر 1391, 19:37 عصر
این کد رو بذار
using System.Threading;
تو Program.cs

bool ok;
Mutex m = new Mutex(true, "نام برنامه", out ok);
if (!ok)
{
MessageBox.Show("برنامه در حاله اجرا است!!");
return;
}

موفق باشی