473,547 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to limit program can only run one instance.

Hi All,

How to limit the c# program can only run one instance at the same
time ?

Thank you!

Best regards,
Boki.

Jul 23 '07 #1
20 10606
"Boki Digtal" <bo******@ms21. hinet.netwrote in message news:11******** *************@x 35g2000prf.goog legroups.com...
How to limit the c# program can only run one instance at the same
time ?

using System.Threadin g;

bool blnFirstInstanc e;

using (Mutex objMutex = new Mutex(false, "Local\\" + "<app name>", out blnFirstInstanc e))
{
if (!blnFirstInsta nce)
{
MessageBox.Show ("<app nameis already running", "Single Use Only", MessageBoxButto ns.OK, MessageBoxIcon. Stop);
Application.Exi t();
}
else
{
Application.Run (new Form1()); // or whatever your startup is...
}
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Jul 23 '07 #2
On 7 23 , 6 11 , "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
"Boki Digtal" <bokit...@ms21. hinet.netwrote in messagenews:11* *************** *****@x35g2000p rf.googlegroups .com...
How to limit the c# program can only run one instance at the same
time ?

using System.Threadin g;

bool blnFirstInstanc e;

using (Mutex objMutex = new Mutex(false, "Local\\" + "<app name>", out blnFirstInstanc e))
{
if (!blnFirstInsta nce)
{
MessageBox.Show ("<app nameis already running", "Single Use Only", MessageBoxButto ns.OK, MessageBoxIcon. Stop);
Application.Exi t();
}
else
{
Application.Run (new Form1()); // or whatever your startup is...
}

}

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net
Great, thanks!

Boki.

Jul 24 '07 #3
On 7 24 , 2 27 , Boki Digtal <bokit...@ms21. hinet.netwrote:
On 7 23 , 6 11 , "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
"Boki Digtal" <bokit...@ms21. hinet.netwrote in messagenews:11* *************** *****@x35g2000p rf.googlegroups .com...
How to limit the c# program can only run one instance at the same
time ?
using System.Threadin g;
bool blnFirstInstanc e;
using (Mutex objMutex = new Mutex(false, "Local\\" + "<app name>", out blnFirstInstanc e))
{
if (!blnFirstInsta nce)
{
MessageBox.Show ("<app nameis already running", "Single Use Only", MessageBoxButto ns.OK, MessageBoxIcon. Stop);
Application.Exi t();
}
else
{
Application.Run (new Form1()); // or whatever your startup is...
}
}
--
Mark Rae
ASP.NET MVPhttp://www.markrae.net

Great, thanks!

Boki.
Is there any easier way like OnlyOneInstance = ture;

Thanks :)

Boki.

Jul 30 '07 #4
"Boki Digtal" <bo******@ms21. hinet.netwrote in message
news:11******** *************@i 38g2000prf.goog legroups.com...
using System.Threadin g;
bool blnFirstInstanc e;
using (Mutex objMutex = new Mutex(false, "Local\\" + "<app name>", out
blnFirstInstanc e))
{
if (!blnFirstInsta nce)
{
MessageBox.Show ("<app nameis already running", "Single Use
Only", MessageBoxButto ns.OK, MessageBoxIcon. Stop);
Application.Exi t();
}
else
{
Application.Run (new Form1()); // or whatever your startup
is...
}
}

Great, thanks!

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...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #5
On 7 30 , 1 51 , "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
"Boki Digtal" <bokit...@ms21. hinet.netwrote in message

news:11******** *************@i 38g2000prf.goog legroups.com...
using System.Threadin g;
bool blnFirstInstanc e;
using (Mutex objMutex = new Mutex(false, "Local\\" + "<app name>", out
blnFirstInstanc e))
{
if (!blnFirstInsta nce)
{
MessageBox.Show ("<app nameis already running", "Single Use
Only", MessageBoxButto ns.OK, MessageBoxIcon. Stop);
Application.Exi t();
}
else
{
Application.Run (new Form1()); // or whatever your startup
is...
}
}
Great, thanks!
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...?

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net
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.

:)

Thanks!

Best regards,
Boki.

Jul 30 '07 #6
On Jul 30, 8:15 am, Boki Digtal <bokit...@ms21. hinet.netwrote:
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.Visua lBasic assembly and
use the WindowsFormsApp licationBase 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

Jul 30 '07 #7
On 7 30 , 3 26 , "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
On Jul 30, 8:15 am, Boki Digtal <bokit...@ms21. hinet.netwrote:
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.Visua lBasic assembly and
use the WindowsFormsApp licationBase 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# ...

Boki.

Jul 30 '07 #8
"Boki Digtal" <bo******@ms21. hinet.netwrote in message
news:11******** **************@ j4g2000prf.goog legroups.com...
On 7 30 , 3 26 , "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
>On Jul 30, 8:15 am, Boki Digtal <bokit...@ms21. hinet.netwrote:
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.Visua lBasic assembly and
use the WindowsFormsApp licationBase 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.Visua lBasic 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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #9
On 7 30 , 4 18 , "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
"Boki Digtal" <bokit...@ms21. hinet.netwrote in message

news:11******** **************@ j4g2000prf.goog legroups.com...
On 7 30 , 3 26 , "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
On Jul 30, 8:15 am, Boki Digtal <bokit...@ms21. hinet.netwrote:
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.Visua lBasic assembly and
use the WindowsFormsApp licationBase 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.Visua lBasic 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...

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net
OK, got it, thanks :)

Boki.

Jul 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2623
by: Bloon | last post by:
I have a question that how to debug a program only having .exe file. Is there any techniques or tools that can do that? Thanks a lot.
4
1950
by: grocery_stocker | last post by:
#include <iostream> //for cin and cout #include <iomanip> // for setw() #include <string> // for strlen() strcmp() strrev() #include <fstream> //ifstream and ofstream: file (input & output) #include <stdlib.h> //for system calls using namespace std; //functions will go here!! unsigned int count_vowels(char *pointer); int main(void)
3
3477
by: Vikrant | last post by:
Friends, Due an application (old) install program problem, Under AIX I could only create DB2 instance, I could also catalog it. Its complex application and I am not expert in creating full database for this application, but I have desired DB2 backup (by db2 backup db command). Can I do redirected restore (SET TABLESPACE CONTAINERS ....) ...
4
7076
by: Josh Burkard | last post by:
Hello How can i control, that a user can't start my VB.Net-Program 2 ore more times at once (maybee like Outlook)? At same time Parameters which are transfered with the second program should be transfered to the first program. Best regards Josh
1
2816
by: Shisou | last post by:
I think the title says it all... i'm building a program that will read in a dictionary and then ask the user for a string and will check all permutations of the string against the dictionary to see if it's a word. but for some reason when i run the program normally it crashes out... but if i run it in debug mode it runs fine... This is the code...
0
7510
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7437
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7463
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7797
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6032
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3493
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
748
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.