# [SOLVED] [C# Net] Running processes



## Emphyrio (May 16, 2009)

I've got this methode:


```
private void ShowRunningProcess()
        {
            // Get a list of all running processe
            StringBuilder logText = new StringBuilder();
            List<string> arRun = new List<string>();
            Process[] processlist = Process.GetProcesses();
            try
            {
                Process[] runningProcesses = Process.GetProcesses();
                var currentSessionID = Process.GetCurrentProcess().SessionId;
                Process[] thisSession = (from c in runningProcesses where c.SessionId == currentSessionID select c).ToArray();
                foreach (var p in thisSession)
                {
                    arRun.Add(p.MainModule.FileName.Replace(@"\??\", ""));
                }
            }
            catch (Exception e)
            {
                //  Log the error
               Messagebox.Show(e.Message);
            }
            arRun.Sort();
            for (int i = 0; i < arRun.Count; i++)
            {
                logText.Append(arRun[i]).AppendLine("");
            }
            logText.AppendLine("");
        }
```
The methode is doing what it supose to do, but, when the pc get too busy I got this error:


> A ReadProcessMemory- or WriteProcessMemory-command is only partialy executed.


I 've read several articles about a flaw in the processes reading that triggers this exception, but the thing is, it also tempers with my results.
The result has to be the running processes.

Any idea's how to speed up or improve this methode so I can avoid that exception and get the correct result?


----------



## Emphyrio (May 16, 2009)

*Re: [C# Net] Running processes*

Anyone ? :sad:


----------



## Emphyrio (May 16, 2009)

*Re: [C# Net] Running processes*

I'll try to *bump* one more time, but I'm getting the feeling that there aren't any C# wizzs present.


----------



## Stephen Bowles (Jan 28, 2011)

*Re: [C# Net] Running processes*

Programmatically, you could use a loop here, if there is an exception, restart the method. This should work if you only get the exception rarely. Can you confirm that it only happens when your system gets busy and not randomly?

My other suggestion, leading on from the last question above, is try running this on a system other than Windows Vista.



BobS0327 said:


> I've encountered the same problem as you with Vista Enterprise edition.
> 
> Vista has new security enhancements to thwart the malware folks. One feature is Address Space Load Randomization which randomly loads a process into memory. Under Vista, you have a 1 in 256 chance of finding the base address of a process. Whereas, under XP and W2K you knew exactly where in memory a process started as indicated above.
> 
> ...


ReadProcessMemory Error


----------



## Emphyrio (May 16, 2009)

*Re: [C# Net] Running processes*

Thanks for the reply, Stephen 

As I stated in my first post: it only occurs when pc is buzzy.
And yes, it seems to be related to Vista only.

It defenly occurs more when running from VS 2010.
So I suspect memory and process handling is the cause of this.

I use this (a bit modified) routine in a tool that I published: DaS 2.1.

I did a bit "alien" testing before publishing and none gave the "error" back.


----------



## Stephen Bowles (Jan 28, 2011)

I have spoken to some others about this and they were unsure, so I apologies for being unable to help further with this, although it seems like you have no solved it yourself?


----------



## Emphyrio (May 16, 2009)

Yes, thank you Stephen 

I think its due of "heavy" processing while using VS 2010.
Could very well be, because the routine is investigating "events" (or threads).

I'm using a processor older then the i3.

The tool (DaS 2.1) does his job sofar


----------



## AceInfinity (Jan 21, 2012)

You're doing many redundant things here. The majority of those arrays are definitely not needed, but you're also misusing the StringBuilder methods.


```
logText.Append(arRun[i]).AppendLine("");
```
If you just want to append a line, you can call AppendLine() without any arguments, no empty string needed, but to append a line with text, why didn't you just put it all in the AppendLine() method instead of calling Append() then having the newline added afterwards with another function call? You only needed this:


```
logText.AppendLine(arRun[i]);
```
Your code could have been simplified to:

```
private void ShowRunningProcess()
{
  var currentSessionID = Process.GetCurrentProcess().SessionId;
  var procList =
    Process.GetProcesses()
    .Where(p => p.SessionId == currentSessionID).ToList();

  StringBuilder logText = procList.Aggregate(new StringBuilder(),
                          (builder, process) => builder.AppendLine(process.MainModule.FileName.Replace(@"\??\", "")));
  logText.AppendLine();
}
```
Although, because you're dealing with modules, I would recommend compiling as x64 if you're on a 64-bit OS.


----------



## Emphyrio (May 16, 2009)

Thanks AceInfinity 

In the final version I placed this routine in a class.


```
public List<string> ShowRunningProcesses()
{
  List<string> rp = new List<string>();
  // Get a list of all running processe
  try
    {
      Process[] runningProcesses = Process.GetProcesses();
      var currentSessionID = Process.GetCurrentProcess().SessionId;
      Process[] thisSession = (from c in runningProcesses where c.SessionId == currentSessionID select c).ToArray();
      foreach (var p in thisSession)
       {
          rp.Add(p.MainModule.FileName.Replace(@"\??\", "") + Environment.NewLine);
        }
       }
       catch (Exception e)
        {
          //  Log the error
         }
   return rp;
 }
```
But your code is looking better. So I gonne replace some of the code above.

I am working on a x86 machine, but tartget the code for "any cpu".


Thanks again for taking the time to reply :thumb:


----------

