Click here to Skip to main content
Licence CPOL
First Posted 20 Jul 2007
Views 8,124
Downloads 29
Bookmarked 7 times

The simplest thread example in the world

By | 20 Jul 2007 | Article
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):

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)

About the Author

rimblock

Web Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNo interêt Pinmemberperrayon7:44 21 Jan '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Jul 2007
Article Copyright 2007 by rimblock
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid