C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, ParameterizedThreadStart
is a delegate that represents the method that will be called when a new thread is started. It allows you to pass a single object as a parameter to the method executed by the thread. In this tutorial, we will cover the following topics:
Let's begin!
First, you need to import the necessary namespaces:
using System; using System.Threading;
Now, let's create a method that will be executed by the thread. This method should have a single parameter of type object
:
public static void PrintNumbers(object count) { int maxCount = (int)count; for (int i = 1; i <= maxCount; i++) { Console.WriteLine(i); } }
ParameterizedThreadStart
allows you to pass a single object to the thread method. If you need to pass multiple values or a complex object, you can use a custom class or a Tuple.
In this example, we'll pass a single int
value to the PrintNumbers
method.
To start the thread, create an instance of the Thread
class with a new ParameterizedThreadStart
delegate pointing to the PrintNumbers
method:
Thread printNumbersThread = new Thread(new ParameterizedThreadStart(PrintNumbers));
To pass the data to the thread, use the Start
method and pass the object as a parameter:
int maxCount = 10; printNumbersThread.Start(maxCount);
Here's the complete example:
using System; using System.Threading; class Program { public static void PrintNumbers(object count) { int maxCount = (int)count; for (int i = 1; i <= maxCount; i++) { Console.WriteLine(i); } } static void Main(string[] args) { Thread printNumbersThread = new Thread(new ParameterizedThreadStart(PrintNumbers)); int maxCount = 10; printNumbersThread.Start(maxCount); printNumbersThread.Join(); // Wait for the thread to complete } }
That's it! You've now learned how to create and start a new thread using ParameterizedThreadStart
in C#. This allows you to pass data to the thread method, which can be helpful when you need to perform operations on specific data in a separate thread. Remember that using threads can introduce potential issues, such as race conditions and deadlocks, so use them with caution and consider alternative approaches like the Task Parallel Library (TPL) for more advanced scenarios.
How to use Process.Start in C#
The Process.Start
method is used to start a new process.
using System.Diagnostics; class Program { static void Main() { Process.Start("notepad.exe"); } }
C# start external process example
Starting an external process (e.g., opening a website).
Process.Start("https://www.example.com");
Passing parameters to a process in C#
Pass arguments to a process.
Process.Start("notepad.exe", "example.txt");
Redirecting standard input/output with Process in C#
Redirecting input and output streams.
ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "cmd.exe", RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true }; Process process = new Process { StartInfo = startInfo }; process.Start(); // Use process.StandardInput and process.StandardOutput
Asynchronous process execution in C#
Execute a process asynchronously.
async Task ExecuteAsync() { using (Process process = new Process()) { // Set process.StartInfo await process.StartAsync(); // Continue with other tasks } }
Working with process information in C#
Retrieve information about a running process.
Process[] processes = Process.GetProcessesByName("notepad"); foreach (Process process in processes) { Console.WriteLine($"Process ID: {process.Id}, Name: {process.ProcessName}"); }
C# Process class and command-line arguments
Accessing command-line arguments.
static void Main(string[] args) { if (args.Length > 0) { Console.WriteLine($"Argument: {args[0]}"); } }
C# Process exit code handling
Handling exit codes after process execution.
Process process = new Process(); process.StartInfo.FileName = "myExecutable.exe"; process.Start(); process.WaitForExit(); int exitCode = process.ExitCode;
Launching elevated processes in C#
Run a process with elevated privileges.
ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "cmd.exe", Verb = "runas", // Run as administrator }; Process.Start(startInfo);
Detecting when a process exits in C#
Handling the Exited
event.
Process process = new Process(); process.Exited += (sender, e) => { Console.WriteLine("Process exited"); };
C# ProcessStartInfo for fine-grained control
Fine-grained control using ProcessStartInfo
.
ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "myExecutable.exe", Arguments = "argument1 argument2", RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true };
Running PowerShell commands from C#
Execute PowerShell commands.
Process.Start("powershell", "Get-Process");
Interacting with child processes in C#
Interact with a child process.
ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "myChildProcess.exe", RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true }; Process process = new Process { StartInfo = startInfo }; process.Start(); // Use process.StandardInput and process.StandardOutput