C# ThreadStart: Create A Thread

C# ThreadStart: Create A Thread

In C#, the System.Threading namespace provides the Thread class and other thread-related classes for creating, managing, and synchronizing multiple threads. The ThreadStart delegate represents the method that will be executed when a new thread is started. In this tutorial, we'll cover the basics of creating a new thread using the ThreadStart delegate.

  • Import the necessary namespaces

First, add the System and System.Threading namespaces to your program.

using System; using System.Threading; 
  • Define a method to be executed by the new thread

Create a method that you want the new thread to execute. This method should have no parameters and return void.

static void PrintNumbers() { for (int i = 0; i < 10; i++) { Console.WriteLine(i); } } 
  • Create a new thread using the ThreadStart delegate

Instantiate the Thread class and provide a ThreadStart delegate, which represents the method that the thread will execute.

Thread newThread = new Thread(new ThreadStart(PrintNumbers)); 
  • Start the new thread

Call the Start method on the Thread instance to start the new thread.

newThread.Start(); 
  • Example

Here's a complete example of creating a new thread using the ThreadStart delegate:

using System; using System.Threading; class Program { static void Main() { // Create a new thread using the ThreadStart delegate Thread newThread = new Thread(new ThreadStart(PrintNumbers)); // Start the new thread newThread.Start(); } static void PrintNumbers() { for (int i = 0; i < 10; i++) { Console.WriteLine(i); } } } 

In this tutorial, we covered the basics of creating a new thread using the ThreadStart delegate in C#. The ThreadStart delegate represents the method that will be executed when a new thread is started. By using the ThreadStart delegate and the Thread class, you can create and manage multiple threads in your C# application.

Examples

  1. How to create and start threads in C#

    using System; using System.Threading; class Program { static void Main() { Thread myThread = new Thread(MyThreadFunction); myThread.Start(); } static void MyThreadFunction() { Console.WriteLine("Thread is running!"); } } 
  2. Thread class in C#

    using System; using System.Threading; class Program { static void Main() { Thread myThread = new Thread(MyThreadFunction); myThread.Start(); } static void MyThreadFunction() { Console.WriteLine("Thread is running!"); } } 
  3. Multithreading in C#

    using System; using System.Threading; class Program { static void Main() { Thread thread1 = new Thread(MyThreadFunction); Thread thread2 = new Thread(MyThreadFunction); thread1.Start(); thread2.Start(); } static void MyThreadFunction() { Console.WriteLine("Thread is running!"); } } 
  4. Thread synchronization in C#

    using System; using System.Threading; class Program { static int sharedValue = 0; static object lockObject = new object(); static void Main() { Thread thread1 = new Thread(IncrementValue); Thread thread2 = new Thread(IncrementValue); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); Console.WriteLine("Final Value: " + sharedValue); } static void IncrementValue() { for (int i = 0; i < 100000; i++) { lock (lockObject) { sharedValue++; } } } } 
  5. C# ThreadPool class

    using System; using System.Threading; class Program { static void Main() { ThreadPool.QueueUserWorkItem(MyThreadFunction); } static void MyThreadFunction(object state) { Console.WriteLine("Thread from ThreadPool is running!"); } } 
  6. Asynchronous programming with threads in C#

    using System; using System.Threading; using System.Threading.Tasks; class Program { static async Task Main() { Task.Run(() => MyThreadFunction()); Console.WriteLine("Main thread is not blocked!"); Console.ReadLine(); } static async Task MyThreadFunction() { await Task.Delay(2000); Console.WriteLine("Thread is running asynchronously!"); } } 
  7. Thread safety and locking in C#

    using System; using System.Threading; class Program { static int counter = 0; static object lockObject = new object(); static void Main() { Thread thread1 = new Thread(IncrementCounter); Thread thread2 = new Thread(IncrementCounter); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); Console.WriteLine("Final Counter: " + counter); } static void IncrementCounter() { for (int i = 0; i < 100000; i++) { lock (lockObject) { counter++; } } } } 
  8. C# ThreadLocal class

    using System; using System.Threading; class Program { static ThreadLocal<int> threadLocalValue = new ThreadLocal<int>(() => 0); static void Main() { Thread thread1 = new Thread(IncrementThreadLocalValue); Thread thread2 = new Thread(IncrementThreadLocalValue); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); Console.WriteLine("Final ThreadLocal Value: " + threadLocalValue.Value); } static void IncrementThreadLocalValue() { for (int i = 0; i < 100000; i++) { threadLocalValue.Value++; } } } 
  9. Thread prioritization in C#

    using System; using System.Threading; class Program { static void Main() { Thread highPriorityThread = new Thread(MyThreadFunction); highPriorityThread.Priority = ThreadPriority.AboveNormal; Thread lowPriorityThread = new Thread(MyThreadFunction); lowPriorityThread.Priority = ThreadPriority.BelowNormal; highPriorityThread.Start(); lowPriorityThread.Start(); } static void MyThreadFunction() { Console.WriteLine("Thread is running!"); } } 
  10. Thread communication using events in C#

    using System; using System.Threading; class Program { static ManualResetEventSlim manualEvent = new ManualResetEventSlim(false); static void Main() { Thread thread1 = new Thread(WaitForEvent); Thread thread2 = new Thread(SendEvent); thread1.Start(); thread2.Start(); Thread.Sleep(2000); // Allow threads to run manualEvent.Set(); // Set the event } static void WaitForEvent() { Console.WriteLine("Thread is waiting for the event."); manualEvent.Wait(); Console.WriteLine("Event received. Thread continues."); } static void SendEvent() { Thread.Sleep(1000); // Simulate some work Console.WriteLine("Event sent."); manualEvent.Set(); } } 
  11. Cancellation in C# threads

    using System; using System.Threading; class Program { static CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); static void Main() { Thread myThread = new Thread(() => MyThreadFunction(cancellationTokenSource.Token)); myThread.Start(); Thread.Sleep(2000); // Allow thread to run cancellationTokenSource.Cancel(); // Cancel the thread } static void MyThreadFunction(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { Console.WriteLine("Thread is running!"); Thread.Sleep(500); } Console.WriteLine("Thread is canceled."); } } 
  12. Thread interruption in C#

    using System; using System.Threading; class Program { static void Main() { Thread myThread = new Thread(MyThreadFunction); myThread.Start(); Thread.Sleep(2000); // Allow thread to run myThread.Interrupt(); // Interrupt the thread } static void MyThreadFunction() { try { while (true) { Console.WriteLine("Thread is running!"); Thread.Sleep(500); } } catch (ThreadInterruptedException) { Console.WriteLine("Thread is interrupted."); } } } 
  13. BackgroundWorker class in C#

    using System; using System.ComponentModel; using System.Threading; class Program { static void Main() { BackgroundWorker backgroundWorker = new BackgroundWorker(); backgroundWorker.DoWork += (sender, e) => MyThreadFunction(); backgroundWorker.RunWorkerAsync(); Console.WriteLine("Main thread is not blocked!"); Console.ReadLine(); } static void MyThreadFunction() { while (true) { Console.WriteLine("Thread is running!"); Thread.Sleep(500); } } } 

More Tags

lidar scala-gatling ssms-2017 set-returning-functions spark-streaming uint8t asp.net-ajax substring subclassing react-hook-form

More Programming Guides

Other Guides

More Programming Examples