Check for another instance og execute

  • Thread starter =?iso-8859-1?B?wWf6c3Q=?=
  • Start date
?

=?iso-8859-1?B?wWf6c3Q=?=

Hi
I would like to enforce users of my program not beeing
able to run more than one instance of it at any given
time.

For example, on start, check if there are any instances of
the program running and if soo bail out ( exit ).

Any hints anyone?
 
W

Wiktor Zychla

Hi
I would like to enforce users of my program not beeing
able to run more than one instance of it at any given
time.

For example, on start, check if there are any instances of
the program running and if soo bail out ( exit ).

Any hints anyone?

read more about Mutex() class.
 
N

NotYetaNurd

I tried a RoundAbout Method but it works....

public static Process pcheck() //function to return an existing process or
null if no process is running
{
Process current = Process.GetCurrentProcess();
Process[] allProcess = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in allProcess)
{
if(process.Id !=current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/","\\")
== current.MainModule.FileName)
{
return process;
}
}
}
return null;
}

// in ur main do this

static void Main()
{
Process ptry = pcheck();
if (ptry!=null)
{
//alert user and exit
// SendMessage(FindWindow(0,"Net Sender"),786,0,0); // i did this inorder
to bring my application to focus which is in iconic mode
}
else
{
Application.Run(new frmname());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top