Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a pid of a process created in C#

Tags:

c#

process

pid

Lets say that I'm trying to create a new process with the following code:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
p.StartInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\AwesomeFile.exe";
p.StartInfo.Arguments = "parameter1 parameter2";
p.StartInfo.CreateNoWindow = true;
p.Start();

and right in the next line, I'll try to get a pid of that process with the following line:

MessageBox.Show(p.Id);

This line is giving me the "No process is associated with this object." error. Any idea as to why this error occurs?

like image 441
screenshot345 Avatar asked Mar 29 '10 17:03

screenshot345


People also ask

How do I find the PID of a process?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column.

What is PID in C programming?

Getting a process' process ID (PID) Every process on the system has a unique process ID number, known as the pid. This is simply an integer. You can get the pid for a process via the getpid system call.

How do I print parent process ID?

Type the simply “pstree” command with the “-p” option in the terminal to check how it displays all running parent processes along with their child processes and respective PIDs. It shows the parent ID along with the child processes IDs.


1 Answers

Check the return value of Process.Start. In some cases, Process.Start can return false, in which case no Id will be associated with it.

like image 123
Reed Copsey Avatar answered Sep 28 '22 03:09

Reed Copsey