Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Proposal: Add new property for getting process command line, environment and parent process #21941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
powercode opened this issue May 24, 2017 · 10 comments
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Diagnostics.Process
Milestone

Comments

@powercode
Copy link

Rationale and Usage

When writing tools, it is often useful to be able to get to information about the processes you manage.
This is easy on Linux with /proc, but harder on windows.
When getting parent process, it is important to check the start time of the process with the PID of the queried process to see if it is older to prevent errors when process ids are recycled.

API proposal

namespace System.Diagnostics
{
     public class  Process : Component
     {
           public string CommandLine { get; }
           public IDictionary<string,string> Environment{ get; }
           public Process ParentProcess { get; }
     }
}

I would suggest returning null if the user doesn't have access to the requested property instead of having to handle exceptions.

@rainersigwald
Copy link
Member

We could use this in MSBuild. We attempt to find worker processes to connect to by enumerating running MSBuild processes and then trying to connect to them.

On Windows + full framework, this is done with Process.GetProcessesByName("MSBuild"), but on core (any OS) the process name tends to be the host . . . so we'll have to try to connect to every dotnet process, instead of filtering to only ones that are running MSBuild. If we had access to the command line, we could be smarter in both cases by attempting to connect only to processes started with the background-process command line parameters.

@powercode powercode changed the title Proposal: Add new property for getting process command line and environment and parent process Proposal: Add new property for getting process command line,environment and parent process Jan 18, 2018
@powercode powercode changed the title Proposal: Add new property for getting process command line,environment and parent process Proposal: Add new property for getting process command line, environment and parent process Jan 18, 2018
@jnm2
Copy link
Contributor

jnm2 commented Feb 13, 2018

IReadOnlyDictionary?

@powercode
Copy link
Author

I think a string is better. There may be many nuances to how a command line is split into it's parts, and I think that should be left to some higher level.

@jnm2
Copy link
Contributor

jnm2 commented Feb 13, 2018

Unless you're not on Windows. Then to get a single string you have to join the native process args, so you lose information.

@powercode
Copy link
Author

In that case, we should be able to do the same thing on windows as .net executables do to get the string[] args in the Main function.

@powercode
Copy link
Author

Oh, now I understand that we may be talking about the environment. And in that case IReadOnlyDictionary seams reasonable.

@jnm2
Copy link
Contributor

jnm2 commented Feb 13, 2018

Ah, sorry! Yes, IReadOnlyDictionary<string, string> for the environment.
string for the command line works well on Windows but what about other operating systems? Do we want two properties like is being added for ProcessStartInfo (https://github.com/dotnet/corefx/issues/23592)?

@powercode
Copy link
Author

it is a string in /proc/

@CJHarmath
Copy link

This would also fix PowerShell Get-Process missing the command line argument
PowerShell/PowerShell#1849

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@adamsitnik adamsitnik modified the milestones: 5.0.0, 6.0.0 Jun 23, 2020
@adamsitnik adamsitnik removed their assignment Aug 12, 2020
@adamsitnik adamsitnik modified the milestones: 6.0.0, Future Jul 26, 2021
@danmoseley
Copy link
Member

@joeloff asked today about getting parent process ID, for an installer scenario. I suppose the argument for returning a Process object vs GetProcessById() is to avoid races with pid recycling.

I wonder whether it would help to consider environment, command line, and parent as separate requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Diagnostics.Process
Projects
None yet
Development

No branches or pull requests

7 participants