简体   繁体   English

Process.Handle 不是常数?

[英]Process.Handle is not constant?

Process.Handle is returning different values every time, is this correct? Process.Handle每次都返回不同的值,这是正确的吗? From msdn, "The handle that the operating system assigned to the associated process when the process was started. The system uses this handle to keep track of process attributes."来自 msdn,“进程启动时操作系统分配给关联进程的句柄。系统使用此句柄来跟踪进程属性。” https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.handle?view=netcore-3.1 https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.handle?view=netcore-3.1

Isn't supposed to be constant if not unique?如果不是唯一的,不应该是恒定的吗?

Tried different ways of getting the process but the Handle is different everytime.尝试了不同的方法来获取该过程,但句柄每次都不同。 Ex:前任:

Process.GetProcesses().FirstOrDefault(...)

OR或者

Process.GetProcessById(123)

OR或者

Process.GetProcessesByName("xyz")

I'm trying to hold a process id of a process that is launched by my application and this id will be used "later" to get the process from the running processes to stop it, if it is still running in a particular case.我正在尝试保存由我的应用程序启动的进程的进程 ID,如果它在特定情况下仍在运行,该 ID 将在“稍后”用于从正在运行的进程中获取进程以停止它。 I don't want to check with name as the same application can be launched externally.我不想检查名称,因为可以从外部启动相同的应用程序。

Trying to add another layer of validation to make sure any other process is not running with same id later(if my expected process is already stopped and the same id is used for restarting same application or any other application)尝试添加另一层验证以确保以后没有使用相同的 id 运行任何其他进程(如果我的预期进程已经停止并且相同的 id 用于重新启动相同的应用程序或任何其他应用程序)

Is the Process.Handle property expected to be varying or Any other way to do the same? Process.Handle属性是否预计会发生变化或任何其他方式可以做到这一点?

It's "a handle for a process" not "the handle for a process".它是“进程的句柄”而不是“进程的句柄”。 It is a quantity that allows a program to operate on a process, not the sole identifier of a process.它是一个允许程序对进程进行操作的量,而不是进程的唯一标识符。

The same is true for any other Windows kernel handle.对于任何其他 Windows kernel 手柄也是如此。 For example, if you open the same file twice by 'the usual methods', you'll have two different handles for the same thing.例如,如果您通过“常用方法”两次打开同一个文件,您将有两个不同的句柄来处理同一件事。

As long as the process has not terminated or there is still one handle open that refers to the process, its process id is the actual unique identification.只要进程没有终止或者仍然有一个句柄打开引用该进程,它的进程ID就是实际的唯一标识。 The id can be reused later however.但是,该 id 可以稍后重用。

I can't answer why the documentation says what it does.我无法回答为什么文档说明了它的作用。

'm trying to hold a process id of a process that is launched by my application and this id will be used "later" to get the process from the running processes to stop it, '正在尝试保存由我的应用程序启动的进程的进程 ID,并且此 ID 将“稍后”用于从正在运行的进程中获取进程以停止它,

Just hold onto the Process instance, then you can skip the "get the process from the running processes".只需按住Process实例,然后您可以跳过“从正在运行的进程中获取进程”。

You need to do this anyway to prevent the process ID from being reused.无论如何,您都需要这样做以防止进程 ID 被重用。 The process ID is constant for the lifetime of the process-tracking kernel data object, which really means "as long as either the process is alive or someone keeps an open handle to it".进程 ID 在进程跟踪 kernel 数据 object 的生命周期内是恒定的,这实际上意味着“只要进程处于活动状态或有人对其保持打开的句柄”。 By keeping a Process object instance, you keep a handle open, and keep the process ID valid.通过保持Process object 实例,您可以保持句柄打开,并保持进程 ID 有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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