B
Boki Digtal
Hi All,
How to limit the c# program can only run one instance at the same
time ?
Thank you!
Best regards,
Boki.
How to limit the c# program can only run one instance at the same
time ?
Thank you!
Best regards,
Boki.
Boki Digtal said:How to limit the c# program can only run one instance at the same
time ?
using System.Threading;
bool blnFirstInstance;
using (Mutex objMutex = new Mutex(false, "Local\\" + "<app name>", out blnFirstInstance))
{
if (!blnFirstInstance)
{
MessageBox.Show("<app name> is already running", "Single Use Only", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
else
{
Application.Run(new Form1()); // or whatever your startup is...
}
}
Great, thanks!
Boki.
Is there any easier way like OnlyOneInstance = ture;
An easier way? The above is only a dozen lines of code...
Can't you get it to work, then...?
Sorry, in fact, I don't have try it yet.
If I recall correctly, there is a setting in VC6, when we assign so
many parameters to create a form. For this purpose, what we need to do
is assign a parameter as true only.
I believe your code can work, but I am just curious if there is any
other methods.
You could add a reference to the Microsoft.VisualBasic assembly and
use the WindowsFormsApplicationBase class which I believe has this
sort of functionality. I don't know much about it, but it *might* make
things easier for you.
Jon
visual basic ... ? but I am using c# ...
Yes, but you can set a reference to the Microsoft.VisualBasic assembly and
use the (without wishing to get into a language war) "hand-holding"
functions ported over from VB6 if the code I suggested (which I pretty much
nicked from Jon anyway) is too much for you...
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
using System.Threading;
bool blnFirstInstance;
using (Mutex objMutex = new Mutex(false, "Local\\" + "<app name>", out blnFirstInstance))
{
if (!blnFirstInstance)
{
MessageBox.Show("<app name> is already running", "Single Use Only", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
else
{
Application.Run(new Form1()); // or whatever your startup is...
}
}
Boki said:Please see my complete "Program.cs" below:
After I did this, it can still run more than one instance.
Yes, because your "using" statement for the mutex doesn't have braces
round the code. You've effectively got:
bool blnFirstInstance;
using (Mutex objMutex = new Mutex(false,"Local\\" + "BokiTesting",
out blnFirstInstance))
{
Application.EnableVisualStyles();
}
Application.SetCompatibleTextRenderingDefault(false);
// Rest of code...
The idea is to hold the mutex for the whole of the time your app is
running.
Yes, because your "using" statement for the mutex doesn't have braces
round the code. You've effectively got:
bool blnFirstInstance;
using (Mutex objMutex = new Mutex(false,"Local\\" + "BokiTesting",
out blnFirstInstance))
{
Application.EnableVisualStyles();
}
Application.SetCompatibleTextRenderingDefault(false);
// Rest of code...
The idea is to hold the mutex for the whole of the time your app is
running.
Boki said:I tried same code on Vista, it seems no work. but works on XP.
Could you post a short but complete program which demonstrates the
problem on Vista?
(You haven't shown us Form1, for example.)
It should work fine on Vista.
Boki said:I am using the same code here for program.cs as below:
////////////////
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool firstInstance;
Mutex mutex = new Mutex(false, "Local\\" + "SPECIAL TEST
8324428", out firstInstance);
if (firstInstance)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("An instance has already been
run!","BOKI BOKI");
}
}
///////////////
Process[] processes =
Process.GetProcessesByName(process.ProcessName);
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.