0
How to make my application run only 1 instance at a time?
void Main(string[] s){
//faz algo
}
0
How to make my application run only 1 instance at a time?
void Main(string[] s){
//faz algo
}
0
I looked deeper into microsoft libraries and found something similar to Mutex
, and interpreted this code to maintain only one instance.
using System.Threading;
namespace MeuNamespace
{
class Program
{
static Mutex mutex = new Mutex(true, "MeuAplicativo");
static void Main(string[] args)
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
// faz algo
mutex.ReleaseMutex();
}
else
{
MessageBox.Show("Já tens uma instancia em execução!");
}
}
}
}
OBS! Don’t forget that you can’t apply Mutex.ReleaseMutex
directly. It must at least do something before it is released, if it doesn’t end up that Mutex is released before and generating an Exception.
Sources: https://msdn.microsoft.com/pt-br/library/system.threading.mutex(v=vs.110). aspx
Reference:
Mutex.WaitOne
:
Blocks the current thread until the current instance receives a signal, using a Timespan to specify the time interval and specify whether to leave the synchronization domain before the waiting time. (inherited from Waithandle.)
Browser other questions tagged c# thread instantiate-object
You are not signed in. Login or sign up in order to post.