Click here to Skip to main content
12,762,900 members (35,896 online)
Click here to Skip to main content
Add your own
alternative version


68 bookmarked
Posted 21 May 2001

Threading in C#

, , 21 May 2001
Rate this:
Please Sign up or sign in to vote.
This application demonstrates the threads in C# using Forms and GUI controls.
<!-- Download Links -->


A thread is a sequence of instructions executed within the context of a process. MultiThreading is achieved when a  program uses multiple execution threads allowing each thread to share the CPU concurrently depending on the priority assigned to these threads. This helps in the optimum usage of System Resources.

Our application shows how multiple threads (in our case two) run on different level of priorities. We will discuss the whole program part by part.

Code Analysis

//Import These System NameSpaces 
       using System;       
       using System.Drawing;
       using System.WinForms;
       using System.Threading;

We make use of the above mentioned NameSpaces for our application.

System.WinForms and System.Drawing are used to handle the Graphical User Interface. The Progress Bar and Buttons we make all come under and all come as a part of the System.WinForms namespace.

By System.Drawing we take care of all the positioning and size of the WinForm controls. Point and Size are the built-in objects supported by the Drawing namespace.

The System.Threading namespace provides classes and interfaces that enable multi-threaded programming. This namespace includes a ThreadPool class that manages groups of threads, a Timer class that enables a delegate to be called after a specified amount of time, and a Mutex class for synchronizing mutually-exclusive threads. I also provides classes for thread scheduling, wait notification, and deadlock resolution.


public class PgBar : Form
    static int tp1, tp2;
    Thread thread , td;
    ProgressBar pg1 = new ProgressBar();
    ProgressBar pg2 = new ProgressBar();
    Button bt = new Button();
    Button bt1 = new Button();
    Button bt2 = new Button();

This is the declaration part of the Application. Here we are basically declaring public variables and objects. We declare two threads using the Thread class and the other control functions like Progress Bar, ComboBox and Buttons.

static ComboBox cb1 = new ComboBox();
ComboBox.ObjectCollection c1 = new ComboBox.ObjectCollection(cb1);

static ComboBox cb2 = new ComboBox();
ComboBox.ObjectCollection c2 = new ComboBox.ObjectCollection(cb2);

Note that the ComboBox declaration is different from those that you observe in ProgressBar and Button. This is because the statement

static ComboBox cb1 = new ComboBox();

just creates an instance of the ComboBox, and if you want to add items to this you will have to declare an ComboBox.ObjectCollection object.

ComboBox.ObjectCollection c1 = new ComboBox.ObjectCollection(cb1);

 The ComboBox.ObjectCollection constructor takes in a ComboBox object, which becomes the owner of the ObjectCollection. Now for inserting items into the ComboBox you will have to insert the items in the ObjectCollection. For Example:


This is the Main function for the application. It creates an instance of this class that is PgBar. And the second statement runs the application.

public static void Main()
    PgBar pgbApp = new PgBar();
public PgBar()
    this.Size = new Size(500,200);
    this.BackColor = Color.Black;
    this.ForeColor = Color.White;
    pg1.Maximum = 300;
    pg1.Minimum = 0;
    pg1.Step = 20;
    pg1.Location = new Point(10,10);
    pg1.Size = new Size(300,20);
    pg1.BackColor = Color.White;
    pg1.ForeColor = Color.Gray;
    pg2.Maximum = 300;
    pg2.Minimum = 0;
    pg2.Step = 20;
    pg2.Location = new Point(10,40);
    pg2.Size = new Size(300,20);
    pg2.BackColor = Color.White;
    pg2.ForeColor = Color.Gray;
    bt.Text = "START";
    bt.Location = new Point(40,80);
    bt.Size = new Size(60,20);
    bt1.Text = "CLEAR";
    bt1.Location = new Point(120,80);
    bt1.Size = new Size(60,20);
    bt2.Text = "EXIT";
    bt2.Location = new Point(200,80);
    bt2.Size = new Size(60,20);
    c1.Insert(1,"Below Normal Lowest");
    c1.Insert(3,"Above Normal");
    cb1.SelectedIndex = 0;
    cb1.Location = new Point(320,10);
    c2.Insert(1,"Below Normal");
    c2.Insert(3,"Above Normal");
    cb2.SelectedIndex = 0;
    cb2.Location = new Point(320,40);

The above block of code defines all the Graphical Controls. We create the controls by giving definitions to all the controls declared during the declaration stage.


The above statement adds the Button bt to the Form. The keyword this here corresponds to the present Form.

//Event Delegation

bt.Click+= new EventHandler(this.timer1_Tick);
bt1.Click+= new EventHandler(this.timer1_Tick);
bt2.Click+= new EventHandler(this.timer1_Tick);

These statements basically define the event handling method for the buttons in the form. When you create an EventHandler delegate, you specify the method that will handle the event, which in our case is - timer1_Tick. Once the delegate is assigned to the event, the handler method is automatically invoked whenever the event is triggered. So once you click any of the buttons the method time1_Tick is invoked.

//timer1_Tick Method : EventHandling Method

The syntax of the EventHandler delegate specifies the signature for the event handler, where sender is the source of the event and e is an Event object. So the EventHandler method timer1_Tick takes in the source that is the object that triggers the method and an event object. The timer1_Tick method's access modifier is protected and it returns void, that is, nothing.

protected void timer1_Tick (object sender, System.EventArgs e)

If the Button bt, which corresponds to Start in the application, is clicked this block of code will get executed.

    pg1.Value = 0;
    pg2.Value = 0;

    tp1 = cb1.SelectedIndex;
    tp2 = cb2.SelectedIndex;

    thread = new Thread(new ThreadStart(Display));
    place(ref thread,tp1);

    td = new Thread(new ThreadStart(Displaytd));
    place(ref td,tp2);

pg1 is an instance of ProgressBar, and pg1.Value is the property of ProgressBar that assigns or gets the value of the current position the ProgressBar. cb1 is an instance of ComboBox, and SelectedIndex indicates the [zero based] index of the currently selected item in the combos list. The application runs the two ProgressBars as two threads and the two ComboBox allows the user to set the priorities at runtime for these ProgressBars. We create instances for two threads - thread and td. The constructor of Thread class takes instance of  a ThreadStart class. The ThreadStart class is a delegate that points to the method that should be executed first when a thread is started. thread invokes Display method and td invokes Displaytd.  The selected value in ComboBox is captured in a static int variables tp1 and tp2. We pass the reference of the threads and the corresponding index value to method place(). place() method will be explained later. <threadobject>.start() starts executing the thread.


If the Button bt1, which corresponds to Clear in the application, is clicked this block of code will get executed. It just assigns the values to both the ProgressBar instances as 0, thereby, clearing both the ProgressBar.


If the Button bt2, which corresponds to Exit in the application, is clicked this block of code will get executed. <threadobject>.IsAlive returns true or false depending on whether the Thread is running or not.  This checking is must as otherwise it throws ThreadStateException. <threadobject>.Abort() method exits the thread. The Application is closed and both the threads (if they are alive) are aborted.

//ThreadStart Invoked Methods

protected void Display()
    for(long i = 0l; i<20000000; i++)
        if(i%1000000 == 0)
            if(pg1.Value != 300)
                pg1.Value += 20;


protected void Displaytd()
    for(long i = 0l; i<20000000; i++)
        if(i%1000000 == 0)
            if(pg2.Value != 300)
                pg2.Value += 20;

Both Display() and Displaytd() methods effectively do exactly the same task. We went in for this because we wanted to display how two identical threads, when assigned different priorities, work. With every step the value of ProgressBars are incremented by 20 till the value of ProgressBars reach 300 and then the thread aborts.

//place( ) Method 
public void place(ref Thread p, int x)
        case 0: 
            p.Priority = ThreadPriority.Lowest;
        case 1: 
            p.Priority = ThreadPriority.BelowNormal;
        case 2: 
            p.Priority = ThreadPriority.Normal;
        case 3: 
            p.Priority = ThreadPriority.AboveNormal;
        case 4: 
            p.Priority = ThreadPriority.Highest;

place() method takes in a reference to a thread and the (int) index value captured through the ComboBoxs as its arguments. Then we assign prioities to the threads according to the value selected in the ComboBox.


Unlike in Java where you can have priorities from 1 to 10 corresponding to MIN_PRIORITY to MAX_PRIOITY. You can assign any priority within this range i.e., 1 to 10. Whereas in C# you have only five of them. Lowest (0), BelowNormal (1), Normal (2), AboveNormal (3) and Highest (4).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Authors

Anshuman Dimri
United States United States
I am an Under Graduate (on the verge of completion of my Grads) from one of the most prestigious Universities in India - BITS, Pilani.

United States United States
dha dnbdk asnjdbnas mdb
djh dkas
d lndl sakd

You may also be interested in...

Comments and Discussions

Questionworking code for vs 2010 Pin
jacob waswa5-Nov-12 1:53
memberjacob waswa5-Nov-12 1:53 
Questionwindows form or console appplication Pin
EstherIfioky1-Oct-12 9:56
memberEstherIfioky1-Oct-12 9:56 
AnswerRe: windows form or console appplication Pin
jacob waswa5-Nov-12 1:31
memberjacob waswa5-Nov-12 1:31 
GeneralRe: Threading in C# Pin
Ankitaaguggi10-Sep-11 0:31
groupAnkitaaguggi10-Sep-11 0:31 
QuestionThreading in C# Pin
awadhendra tiwari30-Aug-11 2:52
memberawadhendra tiwari30-Aug-11 2:52 
GeneralCross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on. Pin
VidhuKumar12-May-11 22:05
memberVidhuKumar12-May-11 22:05 
GeneralRe: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on. Pin
gihankoli24-Nov-11 8:18
membergihankoli24-Nov-11 8:18 
QuestionCross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on. Pin
VidhuKumar12-May-11 22:02
memberVidhuKumar12-May-11 22:02 
GeneralNot much helpfull Pin
Navin C. Pandit14-Sep-09 2:57
memberNavin C. Pandit14-Sep-09 2:57 
GeneralWorking code for Visual Studio 2003 Pin
laurentma9214-Sep-07 1:37
memberlaurentma9214-Sep-07 1:37 
GeneralThis programe can not work in .net2005 Pin
sun_jeri@163.com17-Aug-06 21:52
membersun_jeri@163.com17-Aug-06 21:52 
GeneralRe: solution for you is... Pin
aristo/samar14-Oct-06 15:56
memberaristo/samar14-Oct-06 15:56 
GeneralNo working Pin
XHungChung23-Jul-06 17:56
memberXHungChung23-Jul-06 17:56 
GeneralProvide a link to download the source code! Pin
Anonymous21-Jun-05 10:24
sussAnonymous21-Jun-05 10:24 
GeneralRe: Provide a link to download the source code! Pin
Anonymous21-Jun-05 10:25
sussAnonymous21-Jun-05 10:25 
GeneralIt doesn't work Pin
janigorse17-Jul-04 2:46
memberjanigorse17-Jul-04 2:46 
GeneralRe: It doesn't work Pin
Robgeez6-Aug-04 0:45
memberRobgeez6-Aug-04 0:45 
GeneralRe: It doesn't work Pin
sk8rboy2-Nov-04 10:08
membersk8rboy2-Nov-04 10:08 
GeneralWhy pass Thread as Ref Pin
Satch12-Nov-02 21:58
memberSatch12-Nov-02 21:58 
GeneralRe: Why pass Thread as Ref Pin
jeffshantz23-Nov-04 0:17
memberjeffshantz23-Nov-04 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 22 May 2001
Article Copyright 2001 by Anshuman Dimri, narendar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid