System.Kernel.ProcessAndThread
The ProcessAndThread namespace provides classes, structures, and enumerations that enable developers to create, manage, and synchronize processes and threads on Windows operating systems.
Table of Contents
Overview
This namespace contains the core API surface for interacting with the Windows kernel's process and thread subsystems. It includes functionality for:
- Creating and terminating processes.
- Creating, suspending, resuming, and terminating threads.
- Querying process and thread information.
- Applying security descriptors.
- Synchronizing execution using events, mutexes, and semaphores.
Classes
| Class | Description |
|---|---|
Process | Represents a Windows process and provides methods to start, stop, and query processes. |
Thread | Encapsulates a thread within a process and offers control over its execution. |
ProcessStartInfo | Specifies a set of values that are used when starting a process. |
ProcessThreadCollection | Provides a strongly-typed collection of Thread objects. |
Structures
PROCESS_INFORMATION– Contains handles and identifiers for a newly created process and its primary thread.STARTUPINFO– Specifies the window station, desktop, standard handles, and appearance of the main window for a process at creation.THREADENTRY32– Holds information about a thread in the system snapshot.
Enumerations
ProcessAccessRights– Defines access rights for process objects.ThreadAccessRights– Defines access rights for thread objects.CreationFlags– Controls the behavior of a newly created process.
Code Examples
Creating a Process
using System;
using System.Diagnostics;
using SystemKernel.ProcessAndThread;
class Example
{
static void Main()
{
var startInfo = new ProcessStartInfo
{
FileName = "notepad.exe",
Arguments = "",
UseShellExecute = false,
RedirectStandardOutput = false
};
using var process = Process.Start(startInfo);
Console.WriteLine($"Started Process Id: {process.Id}");
process.WaitForExit();
Console.WriteLine("Process exited.");
}
}
Creating a Thread
using System;
using SystemKernel.ProcessAndThread;
class ThreadDemo
{
static void Main()
{
var thread = new Thread(() =>
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine($"Thread working {i}");
Thread.Sleep(500);
}
});
thread.Start();
thread.Join();
Console.WriteLine("Thread completed.");
}
}
Enumerating Running Processes
using System;
using SystemKernel.ProcessAndThread;
class ListProcesses
{
static void Main()
{
foreach (var proc in Process.GetProcesses())
{
Console.WriteLine($"{proc.Id,5} {proc.ProcessName}");
}
}
}