dx_over_dt
dx_over_dt

Reputation: 14338

.NET get process ID of my application in C#

Is there a way in .NET to get the process ID (or just the Process) of the program making the call?

Process.GetCurrentProcess() gets the Process that's currently active, i.e., the one the user is using right then. I need the Process of my own app which is likely not active at the time I'm calling it.

I could call GetCurrentProcess() on Window.Activated and store it in a private field, but there's no absolute guaranty (I believe) that that event will ever fire if the app is opened in the background.

Upvotes: 2

Views: 6336

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103585

You are mistaken.. GetCurrentProcess returns:

A new Process component associated with the process resource that is running the calling application.

It does not (necessarily) return the process associated with the currently active window.

When the docs refer to the "active" process, that means the currently executing process, i.e. the calling process. The active window may or may not belong to the active process, since of course a process can execute without owning the foreground window, or any window at all.

Upvotes: 1

Related Questions