using System;
using System.Diagnostics;
class MainClass {
static void Main(string[] args) {
int pID = 12345;
Process theProc;
try { theProc = Process.GetProcessById(pID); } catch {
Console.WriteLine("bad PID!");
return;
}
Console.WriteLine("Here are the loaded modules for: {0}", theProc.ProcessName);
try {
ProcessModuleCollection theMods = theProc.Modules;
foreach (ProcessModule pm in theMods) {
Console.WriteLine(string.Format("-> Mod Name: {0}", pm.ModuleName));
}
} catch { Console.WriteLine("No mods!"); }
}
}
CloseMainWindow,WaitForExit
using System;
using System.Threading;
using System.Diagnostics;
class MainClass
{
public static void Main()
{
using (Process process = Process.Start("notepad.exe", @"c:SomeFile.txt"))
{
Console.WriteLine("Waiting 5 seconds before terminating notepad.exe.");
Thread.Sleep(5000);
Console.WriteLine("Terminating Notepad with CloseMainWindow.");
if (!process.CloseMainWindow())
{
Console.WriteLine("CloseMainWindow returned false - " + " terminating Notepad with Kill.");
process.Kill();
}
else
{
if (!process.WaitForExit(2000))
{
Console.WriteLine("CloseMainWindow failed to" + " terminate - terminating Notepad with Kill.");
process.Kill();
}
}
}
}
}
Running another program from your own.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
public class MainClass {
public static void Main() {
Process.Start("notepad.exe", "");
Process.Start("sol.exe", "");
}
}
Start And Kill Process
using System;
using System.Diagnostics;
class MyProcessManipulator {
static void Main(string[] args) {
Process ieProc = Process.Start("IExplore.exe", "www.intertechtraining.com");
Console.Write("--> Hit a key to kill {0}...", ieProc.ProcessName);
try {
ieProc.Kill();
} catch { } // In case user already killed it...
}
}
Get current Process Name
using System;
using System.Reflection;
using System.Diagnostics;
class AssemType
{
public static void Main(string[] args)
{
Process p = Process.GetCurrentProcess();
string assemblyName = p.ProcessName + ".exe";
Console.WriteLine("Examining : {0}", assemblyName);
Assembly a = Assembly.LoadFrom(assemblyName);
Type[] types = a.GetTypes();
foreach(Type t in types)
{
Console.WriteLine("
Type : {0}",t.FullName);
Console.WriteLine(" Base class : {0}",t.BaseType.FullName);
}
}
}
Polynomial all
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
Polynomialall.zip( 11 k)
The simplest polynomial implementation
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
//Compile:
//csc /debug- /o+ /out:polynomial.exe /r:system.dll Counter.cs Driver.cs PolySimple.cs polynomial.cs ipoly.cs
//File:PolySimple.cs
namespace Polynomial
{
using System;
///
///
///
/// This implementation loops through the coefficients and evaluates each
/// term of the polynomial.
///
class PolySimple: Polynomial
{
public PolySimple(params double[] coefficients): base(coefficients)
{
}
public override double Evaluate(double value)
{
double retval = coefficients[0];
double f = value;
for (int i = 1; i < coefficients.Length; i++)
{
retval += coefficients[i] * f;
f *= value;
}
return(retval);
}
}
}
//File:Polynomial.cs
namespace Polynomial
{
using System;
using PolyInterface;
///
///
public abstract class Polynomial
{
public Polynomial(params double[] coefficients)
{
this.coefficients = new double[coefficients.Length];
for (int i = 0; i < coefficients.Length; i++)
this.coefficients[i] = coefficients[i];
}
public abstract double Evaluate(double value);
protected double[] coefficients = null;
}
}
//File:IPoly.cs
namespace PolyInterface
{
///
///
public interface IPolynomial
{
double Eval(double value);
}
}
//File:Driver.cs
namespace Polynomial
{
using System;
using System.Diagnostics;
///
///
public class Driver
{
///
///
/// The polynomial to evaluate
public static double TimeEvaluate(Polynomial p)
{
double value = 2.0;
Console.WriteLine(“{0}”, p.GetType().Name);
// Time the first iteration. This one is done
// separately so that we can figure out the startup
// overhead separately…
long start = Counter.Value;
p.Evaluate(0.0); // do the first iteration.
long delta = Counter.Value – start;
Console.WriteLine(“Overhead = {0:f2} seconds”, (double) delta/Counter.Frequency);
Console.WriteLine(“Eval({0}) = {1}”, value, p.Evaluate(value));
int limit = 100000;
start = Counter.Value;
// Evaluate the polynomial the required number of
// times.
double result = 0;
for (int i = 0; i < limit; i++)
{
result += p.Evaluate(value);
}
delta = Counter.Value - start;
double ips = (double) limit * ((double)Counter.Frequency / (double) delta);
Console.WriteLine("Evalutions/Second = {0:f0}", ips);
Console.WriteLine();
return(ips);
}
///
///
///
public static void Eval(double[] coeff)
{
Polynomial[] imps = new Polynomial []
{
new PolySimple(coeff),
};
double[] results = new double[imps.Length];
for (int index = 0; index < imps.Length; index++)
{
results[index] = TimeEvaluate(imps[index]);
}
Console.WriteLine("Results for length = {0}", coeff.Length);
for (int index = 0; index < imps.Length; index++)
{
Console.WriteLine("{0} = {1:f0}", imps[index], results[index]);
}
Console.WriteLine();
}
///
///
public static void Main()
{
Eval(new Double[] {5.5});
// Evaluate the first polynomial, with 7 elements
double[] coeff =
new double[] {5.5, 7.0, 15, 30, 500, 100, 1};
Eval(coeff);
// Evaluate the second polynomial, with 50 elements
coeff = new double[50];
for (int index = 0; index < 50; index++)
{
coeff[index] = index;
}
Eval(coeff);
}
}
}
//File:Counter.cs
using System;
namespace Polynomial
{
class Counter
{
public static long Frequency
{
get
{
long freq = 0;
QueryPerformanceFrequency(ref freq);
return freq;
}
}
public static long Value
{
get
{
long count = 0;
QueryPerformanceCounter(ref count);
return count;
}
}
[System.Runtime.InteropServices.DllImport("KERNEL32")]
private static extern bool QueryPerformanceCounter( ref long lpPerformanceCount);
[System.Runtime.InteropServices.DllImport("KERNEL32")]
private static extern bool QueryPerformanceFrequency( ref long lpFrequency);
}
}
Polynomial.zip( 2 k)[/csharp]

