473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to Make my programn run for once in C#?

ygy
I want my programn run for once.Forbid the programn run when the
programn has run.I know I can use CWnd::FindWindow() funtion in
C++(MFC).What should I do in C#.Thank you!
Nov 16 '05 #1
2 1369
Hi,

You can take the same approach in c# through P/Invoke layer. You can call to
the FindWindow() fucntion.

If you need a pure c# solution use System.Diagnostic.Process...

static void Main()

{

// get the name of our process

string proc=Process.GetCurrentProcess().ProcessName;

// get the list of all processes by that name

Process[] processes=Process.GetProcessesByName(proc);

// if there is more than one process...

if (processes.Length > 1)

{

MessageBox.Show("Application is already running");

return;

} else

Application.Run(new Form1());

}

Regards,

Nirosh.

"ygy" <yy@spm.com.cn> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I want my programn run for once.Forbid the programn run when the
programn has run.I know I can use CWnd::FindWindow() funtion in
C++(MFC).What should I do in C#.Thank you!

Nov 16 '05 #2
"ygy" <yy@spm.com.cn> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I want my programn run for once.Forbid the programn run when the
programn has run.I know I can use CWnd::FindWindow() funtion in
C++(MFC).What should I do in C#.Thank you!


It was nice and easy in VB with App.PrevInstance, wasn't it...? :-)

I use a Mutex for this in C#...

System.Threading.Mutex objMutex = null;
this.Visible = false;
// check there isn't already an instance of this app
running ------------------------------------------
try
{
bool blnCreatedMutex;
objMutex = new System.Threading.Mutex(true, "<MyAppName>", out
blnCreatedMutex);
if (!blnCreatedMutex)
{
MessageBox.Show("<MyAppName> is already running", "<MyAppName>",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
this.Close();
return;
}
else
{
GC.KeepAlive(objMutex);
this.Visible = true;
}
}
catch (Exception ex)
{
// do your error handling here
return;
}
Nov 16 '05 #3

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

Similar topics

3
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
6
by: Jeremy S. | last post by:
Sorry if this is too far OT - I posted this in the IIS group and got no response - so here goes: What would be a good or recommended way to make a Web site in IIS6 inaccessible to users on the...
6
by: Terry Olsen | last post by:
Is there any way to make my aspx pages appear faster on the first hit? When browse to a page I have on my local server, I watch the server's hard drive light chug away for a good 20-30 seconds...
5
by: CSN | last post by:
I looked through the docs and contrib, but didn't see anything related to storing and using latitude and longitude values. I have data in the form of 12° 34' N, 12° 34' W. Would any of the...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
3
by: Sam Kong | last post by:
Hi group, I want to have some advice about immutable objects. I made a constructor. function Point(x, y) { this.x = x; this.y = y; }
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
3
by: Techsatish | last post by:
how to make a mouseup event called only once during a double click event? here double click is made on a tree node in a tree control. I have the code inside mouseup event....in runtime the...
48
by: Nathan Sokalski | last post by:
Ever since I found out that they didn't give us a way to install both IE6 and IE7 on the same machine, I have been more frustrated and annoyed with Microsoft than I ever have been with any company...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.