简体   繁体   中英

Get custom status of process

I want to allow multiple processes of my application on one machine. Projects (come from local database) can be loaded in each process and I want to ensure that a Project can only be open in one process of my application.

Therefore before opening a Project I want to check if the Project I want to open is already open in another process.

I know that this isn't best programming practice, but I it's just a temporary solution until we implement a more sophisticated locking mechanism on data Level.

I can detect another running process, but can I somehow access or cast it to something more appropriate to get the Information I want ?

Someting like that:

  Process[] allProcesses = Process.GetProcessesByName("MyApp");

  foreach (Process process in allProcesses)
  {
    var projects = process.OpenedProjects;

Have come across .net remoting, WCF, IPC but found that to be very heavyweight for this rather "simple" Task.

If it's a temp solution, just choose the easy way:
When a process load a project, just write a file in the app directory with the project name or ID. If the file already exists it means the project is already loaded in another process. You can also write the process ID as file content, if you want to know which process have opened a project.

Of course you need to delete the file when the process finish. Be sure that you handle all exceptions so that process can't finish without deleting this file. Ie intercept AppDomain.UnhandledException.

If this is a temporary solution as you say, you could give each project an identifier (eg filename) and display it in the title of the main window, for example

C:\\Project1.proj C:\\Project2.proj C:\\Project3.proj

Then check if the project is open with process.MainWindowTitle.Split(' ').Contains .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM