Csharp/CSharp Tutorial/Development/Process

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

All current running processes

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

  public static void Main()
  {
     int totMemory = 0;
     Console.WriteLine("Info for all processes:");
     Process[] allProcs = Process.GetProcesses();
     foreach(Process thisProc in allProcs)
     {
        DateTime started = thisProc.StartTime;
        totMemory += physMemory;
        Console.WriteLine("Process: {0}, ID: {1}", thisProc.ProcessName, thisProc.Id);
        Console.WriteLine("    virtual memory: {0}", thisProc.VirtualMemorySize);
        Console.WriteLine("    private memory: {0}", thisProc.PrivateMemorySize);
        Console.WriteLine("    physical memory: {0}", thisProc.WorkingSet);
     }
     Console.WriteLine("\nTotal physical memory used: {0}", totMemory);
  }

}</source>

Change priority for current process

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

  public static void Main()
  {
     Process thisProc = Process.GetCurrentProcess();
     thisProc.PriorityClass = ProcessPriorityClass.High;
     Console.WriteLine("    new priority class: {0}", thisProc.PriorityClass);
  }

}</source>

new priority class: High

Detecting Process Completion

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

   static void ProcessDone(object sender, EventArgs e)
   {
       Console.WriteLine("Process Exited");
   }
   
   public static void Main()
   {
       Process p = new Process();
       p.StartInfo.FileName = "notepad.exe";
       p.StartInfo.Arguments = "yourFile.cs";
       p.EnableRaisingEvents = true;
       p.Exited += new EventHandler(ProcessDone);
       p.Start();
       p.WaitForExit();
       Console.WriteLine("Back from WaitForExit()");
   }

}</source>

Process Exited
Back from WaitForExit()

Enumerate over mods in a given PID

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

 public static void EnumModsForPid(int pID)
 {
   Process theProc;
   try {
       theProc = Process.GetProcessById(pID);
     } catch {
     Console.WriteLine("-> Sorry...bad PID!");
     return;
   }
   
   Console.WriteLine("Here are the loaded modules for: {0}", theProc.ProcessName);
   try
   {
     ProcessModuleCollection theMods = theProc.Modules;
     foreach(ProcessModule pm in theMods)
     {
       string info = string.Format("-> Mod Name: {0}", pm.ModuleName);
       Console.WriteLine(info);
     }
   }
   catch{Console.WriteLine("No mods!");}
 }
 static void Main(string[] args)
 {
   int theProcID = 10001;
   EnumModsForPid(theProcID);
 }

}</source>

-> Sorry...bad PID!

Enumerate over threads in a given PID

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

 public static void EnumThreadsForPid(int pID)
 {
   Process theProc;
   try {
     theProc = Process.GetProcessById(pID);
   } catch {
     Console.WriteLine("-> Sorry...bad PID!");
     return;
   }
   
   Console.WriteLine("Here are the thread IDs for: {0}", theProc.ProcessName);
   ProcessThreadCollection theThreads = theProc.Threads;
   foreach(ProcessThread pt in theThreads)
   {
     string info = string.Format("-> Thread ID: {0}\tStart Time {1}\tPriority {2}", pt.Id , pt.StartTime.ToShortTimeString(), pt.PriorityLevel);
     Console.WriteLine(info);
   }
 }
 static void Main(string[] args)
 {
   int theProcID = 10001;
   EnumThreadsForPid(theProcID);
 }

}</source>

-> Sorry...bad PID!

Get all processes on local machine

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

 static void Main(string[] args)
 {
   Process[] runningProcs = Process.GetProcesses(".");
   foreach(Process p in runningProcs)
   {
     string info = string.Format("-> PID: {0}\tName: {1}",p.Id, p.ProcessName);        
     Console.WriteLine(info);
   }
 }

}</source>

-> PID: 1636    Name: explorer
-> PID: 1064    Name: svchost
-> PID: 884     Name: svchost
-> PID: 972     Name: svchost
-> PID: 1592    Name: postgres
-> PID: 700     Name: services
-> PID: 1140    Name: svchost
-> PID: 2472    Name: firefox
-> PID: 2560    Name: wuauclt
-> PID: 156     Name: atiptaxx
-> PID: 2908    Name: main
-> PID: 772     Name: wuauclt
-> PID: 1572    Name: ati2evxx
-> PID: 856     Name: cmd
-> PID: 320     Name: cachemgr
-> PID: 2808    Name: uedit32
-> PID: 1472    Name: postgres
-> PID: 848     Name: ati2evxx
-> PID: 1556    Name: postgres
-> PID: 128     Name: SynTPEnh
-> PID: 292     Name: ctfmon
-> PID: 568     Name: smss
-> PID: 656     Name: winlogon
-> PID: 388     Name: ICMON
-> PID: 716     Name: pg_ctl
-> PID: 116     Name: SynTPLpr
-> PID: 1272    Name: svchost
-> PID: 1716    Name: spoolsv
-> PID: 712     Name: lsass
-> PID: 1264    Name: postgres
-> PID: 1348    Name: postmaster
-> PID: 2504    Name: wscntfy
-> PID: 1168    Name: alg
-> PID: 632     Name: csrss
-> PID: 1876    Name: SWNETSUP
-> PID: 2764    Name: SWEEPSRV.SYS
-> PID: 4       Name: System
-> PID: 536     Name: imonitor
-> PID: 624     Name: TNSLSNR
-> PID: 0       Name: Idle

Launch / kill a process

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

 static void Main(string[] args)
 {
   Console.Write("--> Hit a key to launch IE");
   Console.ReadLine();
   // Launch IE.
   Process ieProc = Process.Start("IExplore.exe", "www.nfex.ru");
   Console.Write("--> Hit a key to kill {0}...", ieProc.ProcessName);
   Console.ReadLine();
   try
   {
     ieProc.Kill();
   }
   catch{} // In case user already killed it...
 }

}</source>

--> Hit a key to launch IE
--> Hit a key to kill IEXPLORE...

List all process threads in current running processes

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

  public static void Main()
  {
     Process[] allProcs = Process.GetProcesses();
     foreach(Process proc in allProcs)
     {
        ProcessThreadCollection myThreads = proc.Threads;
        Console.WriteLine("process: {0},  id: {1}", proc.ProcessName, proc.Id);
        foreach(ProcessThread pt in myThreads)
        {
           Console.WriteLine("  thread:  {0}", pt.Id);
           Console.WriteLine("    started: {0}", pt.StartTime.ToString());
           Console.WriteLine("    CPU time: {0}", pt.TotalProcessorTime);
           Console.WriteLine("    priority: {0}", pt.BasePriority);
           Console.WriteLine("    thread state: {0}", pt.ThreadState.ToString()); 
        }
     }
  }

}</source>

process: explorer,  id: 1636
  thread:  1640
    started: 16/03/2007 8:42:07 PM
    CPU time: 00:01:31.1250000
    priority: 8
    thread state: Wait
  thread:  1932
    started: 16/03/2007 8:42:10 PM
    CPU time: 00:02:00.9062500
    priority: 9
    thread state: Wait
  thread:  1936
    started: 16/03/2007 8:42:10 PM
    CPU time: 00:00:00.0156250
    priority: 8
    thread state: Wait
  thread:  1944
    started: 16/03/2007 8:42:10 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1952
    started: 16/03/2007 8:42:11 PM
    CPU time: 00:02:51.6093750
    priority: 8
    thread state: Wait
  thread:  400
    started: 16/03/2007 8:42:17 PM
    CPU time: 00:00:10.0781250
    priority: 8
    thread state: Wait
  thread:  612
    started: 16/03/2007 8:42:24 PM
    CPU time: 00:00:00
    priority: 15
    thread state: Wait
  thread:  1696
    started: 16/03/2007 8:42:52 PM
    CPU time: 00:00:10
    priority: 10
    thread state: Wait
  thread:  2864
    started: 16/03/2007 8:44:36 PM
    CPU time: 00:42:13.9218750
    priority: 8
    thread state: Wait
  thread:  2868
    started: 16/03/2007 8:44:37 PM
    CPU time: 00:00:00.0156250
    priority: 8
    thread state: Wait
  thread:  4032
    started: 20/03/2007 7:32:28 PM
    CPU time: 00:00:00.1250000
    priority: 8
    thread state: Wait
  thread:  208
    started: 23/03/2007 8:22:56 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  2684
    started: 23/03/2007 8:25:29 PM
    CPU time: 00:00:00.0312500
    priority: 8
    thread state: Wait
  thread:  2364
    started: 25/03/2007 12:20:21 PM
    CPU time: 00:00:14.2812500
    priority: 8
    thread state: Wait
  thread:  3964
    started: 25/03/2007 2:25:01 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
process: svchost,  id: 1064
  thread:  1068
    started: 16/03/2007 8:42:03 PM
    CPU time: 00:00:00.6562500
    priority: 8
    thread state: Wait
  thread:  1088
    started: 16/03/2007 8:42:03 PM
    CPU time: 00:00:00.0468750
    priority: 8
    thread state: Wait
  thread:  1092
    started: 16/03/2007 8:42:03 PM
    CPU time: 00:00:00.0781250
    priority: 8
    thread state: Wait
  thread:  1096
    started: 16/03/2007 8:42:03 PM
    CPU time: 00:00:39.6093750
    priority: 8
    thread state: Wait
  thread:  1100
    started: 16/03/2007 8:42:03 PM
    CPU time: 00:00:00.0156250
    priority: 8
    thread state: Wait
  thread:  1132
    started: 16/03/2007 8:42:03 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1240
    started: 16/03/2007 8:42:04 PM
    CPU time: 00:00:00.0312500
    priority: 8
    thread state: Wait
  thread:  1548
    started: 16/03/2007 8:42:07 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1560
    started: 16/03/2007 8:42:07 PM
    CPU time: 00:00:00.1875000
    priority: 8
    thread state: Wait
  thread:  1688
    started: 16/03/2007 8:42:08 PM
    CPU time: 00:00:00.0468750
    priority: 8
    thread state: Wait
  thread:  1692
    started: 16/03/2007 8:42:08 PM
    CPU time: 00:00:02.0468750
    priority: 8
    thread state: Wait
  thread:  1700
    started: 16/03/2007 8:42:08 PM
    CPU time: 00:00:01.5312500
    priority: 8
    thread state: Wait
  thread:  1704
    started: 16/03/2007 8:42:08 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1708
    started: 16/03/2007 8:42:08 PM
    CPU time: 00:00:00.2500000
    priority: 8
    thread state: Wait
  thread:  1772
    started: 16/03/2007 8:42:08 PM
    CPU time: 00:00:17
    priority: 8
    thread state: Wait
  thread:  1820
    started: 16/03/2007 8:42:10 PM
    CPU time: 00:01:06.7031250
    priority: 8
    thread state: Wait
  thread:  148
    started: 16/03/2007 8:42:15 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  380
    started: 16/03/2007 8:42:17 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  392
    started: 16/03/2007 8:42:17 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  396
    started: 16/03/2007 8:42:17 PM
    CPU time: 00:00:00.0156250
    priority: 8
    thread state: Wait
  thread:  424
    started: 16/03/2007 8:42:18 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  628
    started: 16/03/2007 8:42:24 PM
    CPU time: 00:00:00.1093750
    priority: 8
    thread state: Wait
  thread:  1180
    started: 16/03/2007 8:42:25 PM
    CPU time: 00:00:00.0156250
    priority: 8
    thread state: Wait
  thread:  1184
    started: 16/03/2007 8:42:25 PM
    CPU time: 00:00:00.0781250
    priority: 8
    thread state: Wait
  thread:  1256
    started: 16/03/2007 8:42:26 PM
    CPU time: 00:00:01.0156250
    priority: 8
    thread state: Wait
  thread:  1288
    started: 16/03/2007 8:42:26 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1076
    started: 16/03/2007 8:42:26 PM
    CPU time: 00:00:00.0156250
    priority: 15
    thread state: Wait
  thread:  756
    started: 16/03/2007 8:42:26 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  812
    started: 16/03/2007 8:42:26 PM
    CPU time: 00:00:00.0312500
    priority: 8
    thread state: Wait
  thread:  1452
    started: 16/03/2007 8:42:29 PM
    CPU time: 00:00:01
    priority: 8
    thread state: Wait
  thread:  1456
    started: 16/03/2007 8:42:29 PM
    CPU time: 00:00:00.1093750
    priority: 8
    thread state: Wait
  thread:  1480
    started: 16/03/2007 8:42:33 PM
    CPU time: 00:00:00.0468750
    priority: 8
    thread state: Wait
  thread:  1516
    started: 16/03/2007 8:42:33 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1312
    started: 16/03/2007 8:42:33 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1080
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  940
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00.0156250
    priority: 8
    thread state: Wait
  thread:  1160
    started: 16/03/2007 8:42:50 PM
    CPU time: 00:00:00.0156250
    priority: 9
    thread state: Wait
  thread:  1364
    started: 16/03/2007 8:42:50 PM
    CPU time: 00:00:00.0625000
    priority: 8
    thread state: Wait
  thread:  1048
    started: 16/03/2007 8:42:50 PM
    CPU time: 00:00:00.0156250
    priority: 8
    thread state: Wait
  thread:  1368
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00.9687500
    priority: 8
    thread state: Wait
  thread:  1372
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00
    priority: 9
    thread state: Wait
  thread:  1460
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00
    priority: 9
    thread state: Wait
  thread:  1404
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  968
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1536
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1496
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1488
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1500
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1860
    started: 16/03/2007 8:42:51 PM
    CPU time: 00:00:00.0468750
    priority: 8
    thread state: Wait
  thread:  2136
    started: 16/03/2007 8:42:52 PM
    CPU time: 00:00:00.0625000
    priority: 8
    thread state: Wait
  thread:  2148
    started: 16/03/2007 8:42:52 PM
    CPU time: 00:00:01.1406250
    priority: 8
    thread state: Wait
  thread:  2160
    started: 16/03/2007 8:42:53 PM
    CPU time: 00:00:00.5156250
    priority: 8
    thread state: Wait
  thread:  2304
    started: 16/03/2007 8:42:59 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1600
    started: 17/03/2007 8:42:08 AM
    CPU time: 00:00:01.6250000
    priority: 8
    thread state: Wait
  thread:  1972
    started: 17/03/2007 10:17:18 AM
    CPU time: 00:00:01.0156250
    priority: 8
    thread state: Wait
  thread:  1040
    started: 19/03/2007 7:39:40 PM
    CPU time: 00:00:00
    priority: 9
    thread state: Wait
  thread:  160
    started: 20/03/2007 6:05:14 AM
    CPU time: 00:00:01.9218750
    priority: 8
    thread state: Wait
  thread:  1024
    started: 20/03/2007 9:06:56 PM
    CPU time: 00:00:00.0781250
    priority: 8
    thread state: Wait
  thread:  3660
    started: 23/03/2007 8:42:51 PM
    CPU time: 00:00:00.1406250
    priority: 8
    thread state: Wait
  thread:  2072
    started: 24/03/2007 2:36:56 AM
    CPU time: 00:00:00.2031250
    priority: 8
    thread state: Wait
  thread:  732
    started: 25/03/2007 11:18:24 AM
    CPU time: 00:00:00.0781250
    priority: 8
    thread state: Wait
  thread:  3436
    started: 25/03/2007 11:37:24 AM
    CPU time: 00:00:00.0781250
    priority: 8
    thread state: Wait
  thread:  2016
    started: 25/03/2007 12:16:30 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  560
    started: 25/03/2007 12:18:49 PM
    CPU time: 00:00:00.1718750
    priority: 8
    thread state: Wait
  thread:  2856
    started: 25/03/2007 2:10:18 PM
    CPU time: 00:00:00.0468750
    priority: 8
    thread state: Wait
  thread:  2528
    started: 25/03/2007 2:14:25 PM
    CPU time: 00:00:00.0625000
    priority: 8
    thread state: Wait
  thread:  436
    started: 25/03/2007 2:16:38 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  3792
    started: 25/03/2007 2:17:12 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  4088
    started: 25/03/2007 2:17:12 PM
    CPU time: 00:00:00.0156250
    priority: 8
    thread state: Wait
  thread:  1828
    started: 25/03/2007 2:18:54 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  3148
    started: 25/03/2007 2:22:27 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  2320
    started: 25/03/2007 2:24:35 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
process: svchost,  id: 884
  thread:  888
    started: 16/03/2007 8:42:00 PM
    CPU time: 00:00:00.0312500
    priority: 8
    thread state: Wait
  thread:  908
    started: 16/03/2007 8:42:01 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  916
    started: 16/03/2007 8:42:01 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1000
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  944
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  948
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1988
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1984
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1980
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1996
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1992
    started: 16/03/2007 8:42:49 PM
    CPU time: 00:00:00.9062500
    priority: 8
    thread state: Wait
  thread:  2632
    started: 16/03/2007 8:44:10 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  3684
    started: 18/03/2007 4:15:30 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  3652
    started: 18/03/2007 4:15:30 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  1084
    started: 25/03/2007 9:31:28 AM
    CPU time: 00:00:00.0937500
    priority: 8
    thread state: Wait
  thread:  3008
    started: 25/03/2007 1:54:53 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
  thread:  3456
    started: 25/03/2007 2:12:34 PM
    CPU time: 00:00:00
^CTerminate batch job (Y/N)? n

Listing processes on a remote machine

<source lang="csharp">using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime; using System.Runtime.rupilerServices; using System.Security; public class MainClass {

   public static void Main()
   {
       Process[] allProcs = Process.GetProcesses("RemoteMachineOnYourNerwork");
       foreach (Process p in allProcs) 
          Console.WriteLine("  -> {0} - {1}", p.ProcessName, p.PeakWorkingSet64);
   }

}</source>

Property of current Process

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

  public static void Main()
  {
     Process thisProc = Process.GetCurrentProcess();
     Console.WriteLine("ProcessName:"+ thisProc.ProcessName);
     Console.WriteLine("Process: {0}, ID: {1}", thisProc.StartTime, thisProc.Id);
     Console.WriteLine("    CPU time: {0}", thisProc.TotalProcessorTime);
     Console.WriteLine("    priority class: {0}  priority: {1}", thisProc.PriorityClass, thisProc.BasePriority);
     Console.WriteLine("    virtual memory: {0}", thisProc.VirtualMemorySize);
     Console.WriteLine("    private memory: {0}", thisProc.PrivateMemorySize);
     Console.WriteLine("    physical memory: {0}", thisProc.WorkingSet);
  }

}</source>

ProcessName:main
Process: 25/03/2007 2:25:08 PM, ID: 2968
    CPU time: 00:00:00.0312500
    priority class: Normal  priority: 8
    virtual memory: 90906624
    private memory: 9187328
    physical memory: 4984832

Redirecting Process Output

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

   public static void Main()
   {
       Process p = new Process();
       p.StartInfo.FileName = "cmd.exe";
       p.StartInfo.Arguments = "/c dir *.cs";
       p.StartInfo.UseShellExecute = false;
       p.StartInfo.RedirectStandardOutput = true;
       p.Start();
       
       string output = p.StandardOutput.ReadToEnd();
       
       Console.WriteLine("Output:");
       Console.WriteLine(output);    }

}</source>

Output:
 Volume in drive C has no label.
 Volume Serial Number is 8424-900C
 Directory of C:\Java_Dev\WEB\dev\CSharp
25/03/2007  12:31 PM               745 Abaseclassreferencecanrefertoaderivedclassobject.cs
25/03/2007  12:31 PM             3,435 AbetterwaytooverloadandforTwoDimension.cs
25/03/2007  12:34 PM               549 AbortaThread.cs
25/03/2007  12:37 PM               970 Aboutathread.cs

Starting a new process.

<source lang="csharp">using System; using System.Diagnostics;

class MainClass {

 public static void Main() {  
   Process newProc = Process.Start("wordpad.exe"); 

   Console.WriteLine("New process started."); 

   newProc.WaitForExit(); 

   newProc.Close(); // free resources 

   Console.WriteLine("New process ended."); 
 }  

}</source>

New process started.
New process ended.

Starting Processes by using ProcessStartInfo

<source lang="csharp">using System.Diagnostics; class MainClass {

   public static void Main()
   {
       ProcessStartInfo startInfo = new ProcessStartInfo();
       startInfo.FileName = "notepad.exe";
       startInfo.Arguments = "yourFile.cs";
       
       Process.Start(startInfo);
   }

}</source>

Threads in Current Process

<source lang="csharp">using System; using System.Diagnostics; class MainClass {

  public static void Main()
  {
     Process thisProc = Process.GetCurrentProcess();
     ProcessThreadCollection myThreads = thisProc.Threads;
     foreach(ProcessThread pt in myThreads)
     {
        Console.WriteLine("thread:  {0}", pt.Id);
        Console.WriteLine("    started: {0}", pt.StartTime);
        Console.WriteLine("    CPU time: {0}", pt.TotalProcessorTime);
        Console.WriteLine("    priority: {0}", pt.BasePriority);
        Console.WriteLine("    thread state: {0}", pt.ThreadState); 
     }
  }

}</source>

thread:  4064
    started: 25/03/2007 2:25:08 PM
    CPU time: 00:00:00.0312500
    priority: 8
    thread state: Running
thread:  440
    started: 25/03/2007 2:25:08 PM
    CPU time: 00:00:00
    priority: 8
    thread state: Wait
thread:  2444
    started: 25/03/2007 2:25:08 PM
    CPU time: 00:00:00
    priority: 10
    thread state: Wait