Click here to Skip to main content
15,914,109 members
Articles / Programming Languages / C#

The simplest thread example in the world

Rate me:
Please Sign up or sign in to vote.
1.22/5 (12 votes)
20 Jul 2007CPOL 18.7K   63   7   1
Shows the difference between running code on the main form thread, and in a separate thread.

Introduction

This article shows the difference between running a function on the main form thread, and on a separate thread. You can thus still interact with the form even though the code would normally 'block' execution.

Background

I helped a mate out and thought this is a good example as it demonstrates just threading and nothing else.

Using the code

Just download and compile the code. Here is the important code (about five lines below, and is taken from the source code comments):

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    } //do it in another thread

    private void button1_Click(object sender, EventArgs e)
    {
        //create a new instance of YOUR thread class
        ThreadExample te = new ThreadExample();
        te.iIntervalInMilliSeconds = 5000;

        //run the ThreadExample.Start() function in a new thread
        Thread th = new Thread(new ThreadStart(te.Start));
        th.Start();
    }
    //do it in the same thread as your form
    private void button2_Click(object sender, EventArgs e)
    {
        ThreadExample te = new ThreadExample();
        te.iIntervalInMilliSeconds = 5000;
        te.Start();
    }
}

//you need to use classes for this because you probably want
//to set some values before you run the main function 
//(which cant have parameters - unlucky!)
public class ThreadExample
{
    public int iIntervalInMilliSeconds = 1000;
    //all this does is beep every so often - you can still 
    //interact with the form if this runs on another thread
    public void Start()
    {
        for (int iLoop = 0; iLoop < 3; iLoop++)
        {
            Interaction.Beep();
            Thread.Sleep(iIntervalInMilliSeconds);
        }
        MessageBox.Show("End");
    }
}

//NB - if you want to interact with the form from another thread
//you will have to look up 'invoke' as this runs the function you want, but
//on the main form thread (you could have lots of threads all trying 
//to access the same control, so this way, the requests get 'queued up' sort of!

Threading is good when you want to carry on doing something else whilst your computer slaves away in the background.

History

  • V1.0 - The beginning (and the end) - Enjoy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNo interêt Pin
perrayon21-Jan-10 7:44
perrayon21-Jan-10 7:44 

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.