views:

396

answers:

4

what is the procedure to know that when Everytime the user starts the application it will check if an instance is already running. If its not running it will launch it otherwise it will focus to the current one.i have already tried the code for singleton application but its giving lots of errors. its not running properly. can u provide me the othr alternative solution for this??

+1  A: 

You could used a named Mutex.

Named system mutexes are visible throughout the operating system, and can be used to synchronize the activities of processes. You can create a Mutex object that represents a named system mutex by using a constructor that accepts a name.

Taylor Leese
thanks for ur answer..but it is giving me the error for rebuilding my design view again its showing me the error on the design view
zoya
i have gone through the code of mutex.. but it is giving me errorsof public override void dispose(bool disposing); no suitable method found to dispose..this is ocuring on initialize component..so i have commented that portion..but major error im facing is on the design view partThe designer cannot process the code at line 26: throw new NotImplementedException(); The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again. i cannot view form.cs[design]plz help me out!
zoya
please update the question to include the relevant code
Taylor Leese
+1  A: 

Scott hanselman has answered here: http://www.hanselman.com/blog/CommentView.aspx?guid=d2f676ea-025b-4fd6-ae79-80b04a34f24c

Benny
A: 

You should use Mutex class, like explained here: Best way to implement singleton in a console application C#?

EDIT: I have this in my code:

class Program
{
    public const string AppName = "...";
    private static readonly Mutex mutex = new Mutex(true, Program.AppName);

    public Program()
    {
        if (!mutex.WaitOne(TimeSpan.Zero, true))
            throw new ApplicationException("Another instance already running");
    }
}
Rubens Farias
i have gone through the code of mutex.. but it is giving me errors of public override void dispose(bool disposing); no suitable method found to dispose..this is ocuring on initialize component.. so i have commented that portion.. but major error im facing is on the design view part The designer cannot process the code at line 26: throw new NotImplementedException(); The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again. i cannot view form.cs[design] plz help me out!
zoya