[英]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. 已经遇到过.net远程处理,WCF,IPC但发现对于这个相当“简单”的任务非常重量级。
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. 当进程加载项目时,只需使用项目名称或ID在app目录中写入文件。 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.
如果您想知道哪个进程已打开项目,您还可以将进程ID编写为文件内容。
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.
即拦截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
C:\\ Project1.proj C:\\ Project2.proj C:\\ Project3.proj
Then check if the project is open with process.MainWindowTitle.Split(' ').Contains
. 然后使用
process.MainWindowTitle.Split(' ').Contains
. process.MainWindowTitle.Split(' ').Contains
检查项目是否打开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.