Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a .NET 3.5 application and i need it to run only once.
I have already implemented Both a Named Pipes (through System.IO.Pipes)
and a Microsoft.VisualBasic way of dealing with it since i want the second instance to be able to send the cmd args to the first.

Both of those ways are not implemented by Mono yet at least.
Can you provide another way if possible?

Just to remind that the Remoting namespace has changed in 3.5 and does not any more supply a Channels.IPC namespace.
Posted

Heh, its funny some of the complicated solutions people try to use to do simple things like this... Seriously though, no offense to anyone because the answers given so far ARE viable solutions for Microsoft's .NET, but for Mono, the best way to do this and ensure that it remains cross-platform viable is to simply have your application [upon starting an instance] check the contents (or existence) of a file in your applications startup directory to establish whether an instance is already running. There's several ways that you could use such a file, eg. having new instances drop their command-line args. into the file, storing the first instance's Process ID in it, etc...


Jeff T.
 
Share this answer
 
have you tried this way?

Single-Instance C# Application - for .NET 2.0[^]

or

Single Instance Application in C#[^]

which uses a mutex

private static bool IsAlreadyRunning()
{
    string strLoc = Assembly.GetExecutingAssembly().Location;
    FileSystemInfo fileInfo = new FileInfo(strLoc);
    string sExeName = fileInfo.Name;
    bool bCreatedNew;

    mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew);
    if (bCreatedNew)
        mutex.ReleaseMutex();

    return !bCreatedNew;
}

static Mutex mutex;


Neither seem too promising for Mono
 
Share this answer
 
I know this way so that i can make sure that 1 instance is running.
But the code you provided does not handle InterProcess Communication.
How do you suggest i send a message from the 2nd instance to the first, to either Show/Activate itself or handle the 2nd one's Command Line Arguments?

The thing is that .Net 2.0 had a namespace System.Running.Remoting.Channels.ipc for this but .Net 3.5 does not anymore because it has provided another way through System.IO.Pipes and Mono does not support Pipes yet. (Dont even know about Remoting since i am not going to build the app on .Net 2.0 either way).

I have just seen the links you provided before your post.

http://www.codeproject.com/KB/cs/CSSIApp.aspx[^]
This is the Microsoft.VisualBasic Implementation that i already said its not supported by Mono.Net

http://www.codeproject.com/KB/cs/singleinstance.aspx[^]
This uses P/Invoke from windows dll's. Do you really think these wont cause a problem on Linux machines. I am just asking for a Full Managed solution.
 
Share this answer
 
v2
you could send a window message from one app to another.

use the FindWindow / SendMessage / PostMessage api (may have to use pInvoke)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900