简体   繁体   English

获取流程的自定义状态

[英]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.

相关问题 如何在自定义HandleErrorAttribute中获取http状态代码 - How to get the http status code in custom HandleErrorAttribute 有什么方法可以在网页上获取和显示正在运行的服务器进程的状态? - Is there any way to get and show status of a running server process at a web page? 检查流程状态 - Check status of process 如何获得Windows服务自定义状态? [C ++ / C#] - How to get a Windows Service custom Status? [C++/C#] C#CUSTOM VKP80获取打印机状态 - C# CUSTOM VKP80 get printer status c#api status:如果我们从处理api请求所需的某些中间服务获得空响应,应该是什么状态和异常 - c# api status: what should be status and exception if we get null response from some intermediate service required to process api request 如何使用VLC插件获取缓存视频的状态并在c#中修复视频播放器中的自定义徽标 - How to get status of buffering video and fix custom logo in video player in c# using VLC Plugin C#进程是否退出批处理文件状态? - C# Process exit on batch file status? 具有实时接口进度状态的Ajax服务器进程? - Ajax server process with live interface progress status? 连接到正在运行的进程以监视其状态 - Hooking into a running process to monitor its status
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM