DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Basic Of Threading

  • submit to reddit
        C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads.

A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. Here’s a simple example and its output:

All examples assume the following namespaces are imported:

using System;
using System.Threading;

class ThreadTest
  static void Main()
    Thread t = new Thread (WriteY);          // Kick off a new thread
    t.Start();                               // running WriteY()
    // Simultaneously, do something on the main thread.
    for (int i = 0; i < 1000; i++) Console.Write ("x");
  static void WriteY()
    for (int i = 0; i < 1000; i++) Console.Write ("y");


<a href=""><strong>Spring Framework</strong></a>