Getting information of Process Close Reason - c#

Suppose I have an Application.exe running on Windows. I want to log "My process was killed.", if someone else kills my process using task manager etc. How can I get the process closure information? Search results did not return any useful information. I am interested in C# and Windows solutions.
System.Diagnostics.Process class has an ExitCode property. But if process is already killed, how can i even use it?

Related

Why GetProcesses() doesn't show all of running processes

when I run the following code, I expect to get the name of all processes running in the system, but in some other PCs than mine, there is a running process that is visible in the TaskManager but my app doesn't show it, I wanna know if I'm doing something wrong or there is any way to solve this, BTW my app is running as Administrator so that's not the problem. Thanks for your help.
foreach(Process Proc in Process.GetProcesses()) {
T += Proc.ProcessName + ", ";
}
Multiple OS services can be loaded within the same instance of the Service Host process (svchost.exe). GetProcesses does not identify those individual services; for that, use GetServices. Be sure to read the official docs.
Process.GetProcesses() does not return system processes. Services may also appear as svhost.exe.

issue with starting and a killing a process in windows 7

i am trying to start a browser instance as a process from a c# code. then i want to kill the same instance of the browser. I tried finding the same instance with process id . But the process ids are different in task manager and the initial id which i got when i started the process.
what's the solution? why is this happening? Development enviorment is windows 7.
int ID= 0;
void Start()
{
ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe");
startInfo.Arguments = "http://www.google.com";
Process ieProcess = Process.Start(startInfo);
ID= ieProcess.Id;
}
void Stop()
{
foreach (Process p in System.Diagnostics.Process.GetProcessesByName("iexplore"))
{
if ((p.Id == ID))
{
p.Kill();
}
}
This code will not work if IE is already launched. Close all IE browsers and then try to run the code. If it works then you may have to look for solution suggested in following link
similar post-
Process.kill() denied in Windows 7 32bits even with Administrator privileges
Why don't you add your code to the question? It'll make life easy for the people who are interested in helping you. If you get different PIDs, most probably there's something wrong with your code! (I'm just guessing without seeing what you have tried.)
Have a look at these questions as well.
1) Getting PID of process started by Process.start()
2) Programmatically kill a process in vista/windows 7 in C#
3) Process.kill() denied in Windows 7 32bits even with Administrator privileges
Adding the code makes it much easier to understand what the problem is and here's your problem.
IE creates more than one process for one instance of the program. (more details about it) That's why you get different PIDs (for the different processes).
What your code does is killing only one process of it (by the usage of if condition in the Stop() method!). So the remaining process may generate InvalidOperationException when you try to execute Start() again(starting the same process)!
So what your code should do is kill all the active iexplore processes. This can be done by simply removing the if condition of Stop() method.
foreach(Process p in Process.GetProcessesByName("iexplore"))
{
p.Kill();
}
Let me know whether this worked.
I have a similar issue, only I dont want to kill the IE process that I started, I want to bring it into focus.
I have one app that starts 5 IE windows.(not tabs, but unique windows)
I store the PIDs that I start each of the IE windows with.
At particular times, I want to be able to:
select a PID,
find the IE window related to that PID
bring it into focus (minimizing the others)
This worked using XP and IE6 (required for the environment)
Now when I am using Win 7 and IE 8, the PID that I stored is not found,
and thus I no longer have the ability to change the window in focus.

What Does No such interface supported Mean

I am getting an exception on my server side code, which is serving up a silverlight app,
Win32Exception - No such interface supported
Our server side C# code starts up a separate process for a short task because of a third party dll not being thread safe. So the error above occurs in part of the code like this,
Process process = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.CreateNoWindow = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.FileName =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "PreviewGenerator.exe");
process.StartInfo = processStartInfo;
process.Start(); // THIS IS WHERE THE EXCEPTION OCCURS
process.WaitForExit();
The PreviewGenerator.exe process does not start when it is not working, the exception occurs where the comment is above.
UPDATE:
I have run process monitor on the IIS server when the issue occurs. This shows that the w3wp process does this,
Thread Create
Access the file PreviewGenerator.exe
Hive unloaded (this is the registry)
Thread Exit
And it does this before calling the other process. If I compare this with a the process monitor log when it is working it does this,
Thread Create
Access the file PreviewGenerator.exe
Process Start
Does heaps of stuff with PreviewGenerator.exe including reading / writing / registry, etc.
Process Exit
Hive unloaded
Thread Exit
But process monitor does not show any information as to why the first case doesn't work.
Is there a way I can see why the thread exits prematurely?
Also I think this problem relates to when my server is being loaded up more, and much more memory is being used. How can I prove this?
I had a similar issue, I used
processStartInfo.UseShellExecute = false;
and that fixed it for me.
http://www.progtown.com/topic31343-process-start-processstartinfo-startinfo.html
I found the best thing to do was to create a separate app pool for my application in IIS and set an upper limit for the amount of RAM it could use. Also I found it useful to turn on the 'Generate Recycle Event Log Entry' items under the app pool settings.
You can then go to the system event log and filter out the items with a source of 'WAS' to understand what is going on in the app pools, when they are restarting and when they stop from being idle etc.
I think the main problem in our case is that the IIS box was running out of memory. Tuning the app pools and adding some extra RAM seems to have solved it.

Kill method of process not working

I am trying to test the following code snippet.
static void StartAndKill()
{
Process ieProc = Process.Start("iexplore.exe", "www.apress.com");
Console.WriteLine("--> Hit enter to kill {0}\t", ieProc.ProcessName);
Console.ReadLine();
try
{
Console.WriteLine(ieProc.Id);
ieProc.Kill();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
This should kill the internet explorer window and close it. Instead I get an exception that says:
Cannot process request because the process (7256) has exited.
What is the logical explanation for this behaviour?
If the iexplore process is already running (check Task Manager), then Process.Start may use the existing process - but the process id you get back is not for this existing process, but a dummy process that starts and then exits. You could enumerate the existing iexplore processes and check their titles to find the correct one, and then get its process id and kill it.
The error message is misleading. The real reason why the process doesn't get killed is administrative privs on Windows 7 are not defaulted. If I open a command prompt by using "Run as administrator", the code does what it is supposed to do. I will move to close this question.
Thanks to all those who responded.

how to find all application/Thread running in a process in windows application in c#?

i want to get list of all application or Threads attached with a process.For example when we open different window all run with explorer.exe or we open different window of mozila all are in firefox.exe.
i have to check that if a window is all ready open no need to open this.and if it is in background or minimized then then activate the window.
To get all processes
Process.GetProcesses();
or
Process.GetProcessByName("Name"); //To get a process
or
Process.GetCurrentProcess() //To Get the current running process
Once you get your process you have an attribute called Threads.
Is that what you are looking for?
Once you get the Process you can use ProcessName and once you get the ProcessThread you can use ID.

Categories

Resources