1

I need to ban an application if it starts from the same folder, but allow it if the same application runs from other folders.

The problem is when application closes it's becomes invisible but still is in the memory because it terminates some internal job.

It's very possible that user will start this application again from the same folder quickly when the old instance still terminates in the memory.

But from the other side it should be possible if this application runs from other folders.

Any clue how to do it in C#?


UPDATES:

1

In fact application writes some logs into the local files in subdirectory and into the local database file as well. So it's very possible that it could be some conflict between 2 instances.

2

 Guid appGuid = Guid.Parse("305BACEA-4074-11E1-85E1-066E4854019B");

        public MainWindow()
        {
            InitializeComponent();


            using (Mutex mutex = new Mutex(false, @"Global\" + appGuid) )
            {

                if (!mutex.WaitOne(0, false))
                {
                    // MessageBox.Show("Instance already running");

                    // Somehow here I have to get the path of the running instance.
                    // If the path the same as the current instance has I have do ban starting instance.

                    return;
                }

                GC.Collect();              
            }
Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 3
    Close to a repeat of a common question. You can use a named Mutex based on the application path http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx http://stackoverflow.com/questions/819773/run-single-instance-of-an-application-using-mutex – Lloyd Jan 16 '12 at 18:31
  • 3
    This sounds a bit like a clumsy solution to an unspoken problem. If you explained the reason *why* two instances cannot run from the same folder (or be launched from the same executable, by extension) then perhaps it would be possible to propose a better solution for you. – J... Jan 16 '12 at 18:31
  • @Lloyd It's unclear how to detect the path of the running application. – NoWar Jan 16 '12 at 18:34
  • @J Sure I can explain. Because one instance can finish the writing of some file and other instance from the same folder can start to delete it. – NoWar Jan 16 '12 at 18:36
  • 3
    @Dmitry You have the program writing files in its own directory? Not good... – Andrew Barber Jan 16 '12 at 18:43
  • @Dmitry - a temporary file? A file which a new instance will delete after the other instance closes anyway? A file which must be saved for some purpose? – J... Jan 16 '12 at 18:43
  • @Andrew Barber it writes files in subdirectory. – NoWar Jan 16 '12 at 18:44
  • You can use Reflection to get the GetEntryAssembly name and Path, use this as the parameter for your named Mutext. Sorry I am not at my pc to be able to link properly. http://msdn.microsoft.com/en-us/library/aa457089.aspx#howtoexecutingapppath_topic3 – Lloyd Jan 16 '12 at 18:46
  • @Dmitry That's no different at all. – Andrew Barber Jan 16 '12 at 18:47
  • @J... Its a pretty common scenario – Lloyd Jan 16 '12 at 18:51
  • @Lloyd You said: " Reflection to get the GetEntryAssembly name and Path, use this as the parameter for your named Mutext." Can you pls provide some code? The link you've gave me doesn't help much... – NoWar Jan 16 '12 at 19:00
  • http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in – Lloyd Jan 16 '12 at 19:04
  • @Lloyd But how to associate it with MUTEX? I mean http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in – NoWar Jan 16 '12 at 19:08
  • Use the Assembly Executing path as the value for the Named Mutex. In the following example replace the "AppDomain.CurrentDomain.FriendlyName" with the Executing Assembly Path. The mutex will then be unique to that Directory. – Lloyd Jan 16 '12 at 19:18
  • 1
    @Lloyd - single instance is a pretty common scenario. VS handles single instance easily in the application settings. But if the application is fine to run as multiple instances then it shouldn't matter whether those instances are launched from a single executable or from two copies of the same - there is a programmatic solution which, I'm sure, more elegantly resolves any conflicts here. – J... Jan 16 '12 at 19:23
  • @Lloyd: This code string path = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); using (Mutex mutex = new Mutex(false, path)) It gives an error: The error: 'The invocation of the constructor on type 'MyApp.Player.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'. – NoWar Jan 16 '12 at 19:28

2 Answers2

2

Thank you all of you!

Finally based on this post I found the solution:

public partial class App : Application
    {
        private Mutex _instanceMutex = null;

        protected override void OnStartup(StartupEventArgs e)
        {

            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).Replace("\\", ".");

            // check that there is only one instance of the control panel running...
            bool createdNew;
            _instanceMutex = new Mutex(true, path, out createdNew);
            if (!createdNew)
            {
                _instanceMutex = null;
                MessageBox.Show("Instance already running");
                Application.Current.Shutdown();
                return;
            }

            base.OnStartup(e);
        }

        protected override void OnExit(ExitEventArgs e)
        {
            if (_instanceMutex != null)
                _instanceMutex.ReleaseMutex();
            base.OnExit(e);
        }

}
Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498
0

You could use a lock file to indicate that the program is already running on the folder.

When the program starts, it checks for the file in the directory. If it finds it, it shuts down, if not, it creates one and runs as normal. When the program is done, it should delete the file.

This is of course not fool proof. The file will not be deleted if the program crashes, and a user could delete the file while the program is running, bypassing the initial check.

Brian S
  • 1,051
  • 9
  • 10
  • Thank you! Yes it's possible to implement it. But I am pretty sure that there is a way to detect the staring application instance path by .net classes... – NoWar Jan 16 '12 at 19:05
  • Not sure why this was down voted. The solution I provided is acceptable if the application can deal with the problems I indicated. It is the same method Lucene uses to lock a search directory, making sure only one instance is writing to the directory. – Brian S Jan 16 '12 at 19:26