Question

I have a c# application that is on a shared folder in which I have 3 or 4 people who all run the same .exe file. I need to be able to count all instances of the running process.

So far I've tried mutex and:

Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length;

those work if I'm trying to compute the number of instances of the .exe file I have running on my machine, but this will be running on multiple machines all pointing back to the same shared executable.

I don't want to create a file that increments and decrements depending on if a user opens or closes it for that would be unreliable.

--------edit--------------

Does anybody know how Excel does it? If the 2nd person opens the file it says that it is in use and tells you who is using it Thank you for your help!!!

Était-ce utile?

La solution 2

You can tell who has a share open:

open computer management (right click my computer and select manage) then from the tree view on the left select:

system tools> shared folders> sessions

this lists who is accessing files through shares.

there is an SO answer about sdoing this programatically:

https://stackoverflow.com/a/2418657/359135

If you really wanted to take it further and you have the admin rights you could use this info to interrogate running processes on connected machines:

tasklist.exe /S SYSTEM /U USERNAME /P PASSWORD

http://www.watchingthenet.com/how-to-view-and-kill-processes-on-remote-windows-computers.html

I am not suggesting you do any of this.. more to highlight what would be required. I would run a server somewhere that received a open, close and ping notification from your app.

the ping notification would let you identify instances that had crashed, lost connectivity or for any other reason not sent a close notification to the server app.

I have seen this done just by having a table in a DB of open session that gets written to every minute or so. I don't know how this would work for millions of users but i have seen it working well for tens of users (up to about a hundred).

EDIT:

more on pinging..

if you have server side code you can actively look for missing pings, however I would be tempted to just use a DB table and add a where clause to your select when reading open sessions:

Select
    *
FROM
    Session
WHERE
    LastPing > DATEADD(second,-60,Now())

and I would check for an old record on insert, so that you don't get lots of old rows hanging around.

EDIT: just to be clear if you used the crazy techniques listed at the start of this answer you would have no way of knowing that someone had copied the file to their machine locally and run it. If you make the program insist that it has a connection to a particular DB or server then you have much more control.

Autres conseils

There isn't any way other than to implement some form of a licensing service or if you have privileges to the running machines.

A process on one computer cannot detect processes running on a different computer unless you set up some kind of communication between them.

You could set up a shared file as you suggest, but yes it will be unreliable.

You could run a service on some central computer and each instance has to report back to the server, but of course that will have similar reliability issues. (If one instance dies without notifying the server, the server won't realize it's still running.)

When each instance runs, it could listen at a port, record its IP and port number in a central file, and when you need a count you try to connect to each of those ports to confirm that instance is still running.

There are lots of ways you can do it, but the work is up to you. No help from the OS like you get when they're all on the same machine.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top