-2

I have a form application written in C#. I put this exe to a server in my company that is reached from all computers. I need to find a solution to know if this exe is running in another computer.

So when one computer tries to run the application I should check if another computer is already running the same app.

I found something in SO but they are working fine only if the same computer runs the exe two times.

Here

Community
  • 1
  • 1
Mtok
  • 1,600
  • 11
  • 36
  • 62
  • 1
    What do you mean by **"that is reached from all computers"**. How exactly other computers access that exe. – Ricky Jun 26 '14 at 06:50
  • You're going to have to set up something which you can flag. You *could* use a file on the same network share, and simply issue a file lock on it, but unfortunately locks on network shares are somewhat flaky so you're not *guaranteed* this will work. It may be good enough though. Other than that, is there something like a shared database you can store a value in? – Lasse V. Karlsen Jun 26 '14 at 06:50
  • @Ricky exe is in a server. \\server\blabla\app.exe – Mtok Jun 26 '14 at 06:52
  • In general, you can use some shared resource (network shared folder with some file, database, etc.) that is used by your program to 'mark' it as running. You program should check this flag while starting. – Konrad Kokosa Jun 26 '14 at 06:52
  • Ok. Just write an ini file on that server and put a flag in it. Read that flag in your exe. When your exe runs for the first machine write that flag "1" and when your application exits, write that flag "0". In your exe code check that flag value. If it is "1", don't execute your exe further. – Ricky Jun 26 '14 at 06:53
  • @LasseV.Karlsen yes there is. I thought to save a data if someone runs the exe but I'm not sure if it works to inform the database when the program crashes or something. – Mtok Jun 26 '14 at 06:55
  • `Environment.MachineName` might be helpful to get the name of the computer that runs the application. – Hassan Jun 26 '14 at 07:00
  • @Ricky what if the program crashes after I put "1" into file ? (Maybe just power cut) So I can not clear it and no one can run program. – Mtok Jun 26 '14 at 07:03
  • 1
    I think you'll find that no matter which method you choose there will either be a way to circumvent it (block the firewall on a computer so that it cannot inform the world that it is running your program) or a way to break it (leaving the flag set after the program has crashed). **Why** do you need this functionality? – Lasse V. Karlsen Jun 26 '14 at 07:11
  • 2
    @Mtok Just open a file and never close it (No need to write anything). Second instance will not be able to open it and throw exception. If your program crashes/exits, a new instance can open that file. – L.B Jun 26 '14 at 07:39
  • You could create a simple client/server based architecture, create a windows service on the server that listens for clients on a specific network port. you'd then modify your client software to connect to the server port during load. If clients were not issuing a regular "ping" (every 5 seconds say) to the server, then it's safe to assume that they're not connected. – Phill Jun 26 '14 at 07:40
  • @LasseV.Karlsen I dont want another one can run it when already running in somewhere else. – Mtok Jun 26 '14 at 12:26

1 Answers1

2

Have a file writable by your users on the server. When the application starts, open that file for write and keep a reference to the Stream in a static field. When the application is closed, close the Stream. If the application crashes or the network is down, the file will be automatically released by the OS.

For instance, in your Program.cs :

private static Stream lockFile;
public static void Main()
{
  try
  {
    try
    {
      lockFile = File.OpenWrite(@"\\server\folder\lock.txt");
    }
    catch (IOException e)
    {
      int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
      if (err == 32)
      {
        MessageBox.Show("App already running on another computer");
        return;
      }
      throw; //You should handle it properly...
    }

    //... run the apps
  }
  finally
  {
    if (lockFile != null)
      lockFile.Dispose();
  }
}

Based on

Community
  • 1
  • 1
Guillaume
  • 12,824
  • 3
  • 40
  • 48