C# Simple Thread
Introduction
In this example we will create a Simple Thread that will show the basics of
thread creation. We will generate a CSV file with a worker thread and then have
the OS open it. To run this example you must have a CSV view installed.
These instructions and project files are for Visual Studio 2008.
while they may work in older versions of Visual Studio you'll be on your own to adapt them.
Instructions
Start Visual Studio and create a new project. Under project type select
Visual C# and choose Windows Forms Application from the templates.
Name your project and choose a directory to store you project. In this case I
named the project SimpleThreadExample.
Click "OK"
Visual Studio will create the project.
By default Visual studio names the form Form1. Lets change that.
In the Solution Explorer right click on Form1.cs and select "Rename".
Change the name to FormMain.
Select the form in the designer.

In the Properties view scroll down to the "Text" property.

Change the "Text" property to "Simple Thread Example"

Now go back to the form designer and lets make the form wider so we have room
for controls and a feedback area.
In order to resize the form in the designer look for the small white square
on the right side of the form. Single click it and drag.

Next we are going to need a button to run our thread. In the toolbox expand
Common Controls and grab the "Button" control and drag it onto the upper left of
the form.


Select the button and change the Name property to btnRun and the Text property to "Run".

Double click the Run button and the designer will add code for the button click event.

Now we need someplace for feedback to go. We will make a place to print
messages.
Switch to the form designer.
From the tool box drag the TextBox control onto you form.

Change the multiline property to "True", the dock to the bottom of the form and
read only "True".

Rename the control to txtFeedback and set ScrollBars to Vertical.
Now select the txtFeedback control and grab the sizing square at the top and
make the control larger.

Switch to the code view and we will add a Print Out Function. Under the code
designer created for the run button put the following code.
private void PrintOut(string sStr)
{
string sTemp = txtFeedback.Text;
txtFeedback.Text = sStr + "\r\n" + sTemp;
}
When you call this function it will print text messages in the feedback box at
the bottom of the form.
Lets put a test Print Out in the run button click event function.
private void btnRun_Click(object sender, EventArgs e)
{
PrintOut("Test");
}
Hit the compile and run button and click the "Run" button on the form. You
should see this"

Good, you can remove
PrintOut("Test");
We don't need it now. It was just for testing the feedback.
Now it's time to make that thread you have been waiting for. This thread class
will contain all the code for a long running process. For our purposes we will
be generating a CSV file, writing it to disk and then asking the OS to open it
in it's default program for CSV. Typically this will be Excel but there are
other spreadsheet programs out there. If you don't have a spread sheet program
installed you can have the file open in notepad.
Right click on the SimpleThreadExample project name select "Add" and "New Item"
from the drop down menu.
Select "Class" from the right hand panel. In the name area put
CMySimpleThreadClass.cs.

Click the "Add" button. This will create the file and skeleton code for a class
for you.
In the Solution Esplorer double click on CMySimpleThreadClass.cs. We need to
create a function in the class that does the work. Lets call it DoWork.
// This method that will be called when the thread is started
public void DoWork()
{
StreamWriter sw = new StreamWriter("C:\\Test.csv");
int iRowCount = 10000;
int iColCount = 100;
for (int iRow = 0; iRow < iRowCount; iRow++)
{
string sLine = "";
for (int iCol = 0; iCol < iColCount; iCol++)
{
sLine = sLine + iRow.ToString() + "_" + iCol.ToString();
if (iCol < iColCount - 1) sLine = sLine + ",";
}
sw.WriteLine(sLine);
}
sw.Close();
System.Diagnostics.Process.Start("C:\\Test.csv");
}
As you can see we are just using a stream writer to wrote text to a file on the
C drive. We are filling a text file with numbers separated by commas, hence a
Comma Separated Value file or CSV. This will give us a long process to do in the
background thread. Writing large files is a common task you will need to do as a
programmer and being able to keep your GUI from freezing while the file is
written is important.
In order to use StreamWriter will need to add an include to the top of the file.
Since this is going to be a thread lets go ahead and add the Threading header as
well. Place the following code at the top of CMySimpleThreadClass.cs.
using System.Threading;
using System.IO;
Add The threading header
using System.Threading;
to the top of FormMain.cs since we will be calling threads from there as well.
In order to keep track of the status of the thread we will add a timer to out
form.
Switch back to the form designer. from the toolbox drag a Timer control onto the
form and name it timerCheckWorker.

Set the timer interval to 1000. This will fire once a secound.
On the timerCheckWorker properties sheet switch to events. (Click the little
lighting bolt at the top of the property sheet). You will see the Tick event
listed. Double click the Tick event and and event handling function will be
created for you. We will come back to this in a minute after we create instances
of our thread class.
Open CodeMain.cs in code view. At the top of the partial class add these class
variables.
private CMySimpleThreadClass threadWorker;
private Thread aThread;
In FormMain() put right under InitializeComponent(); put:
threadWorker = new CMySimpleThreadClass();
Now back to the timer function. In timerCheckWorker_Tick put the following code.
if (aThread.IsAlive)
{
PrintOut("Thread Running");
}
else
{
PrintOut("Thread Done");
timerCheckWorker.Enabled = false;
}
This will check if the thread is alive and report it's findings to the
feedback box.
The only thing left to do is start the thread when the user presses the "Run"
button.
In btnRun_Click add the following code:
PrintOut("Start Thread");
aThread = new Thread(new ThreadStart(threadWorker.DoWork));
aThread.Start();
timerCheckWorker.Enabled = true;
Here is what you FormMain.cs should look like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace SimpleThreadExample
{
public partial class FormMain : Form
{
private CMySimpleThreadClass threadWorker;
private Thread aThread;
public FormMain()
{
InitializeComponent();
threadWorker = new CMySimpleThreadClass();
}
private void btnRun_Click(object sender, EventArgs e)
{
PrintOut("Start Thread");
aThread = new Thread(new ThreadStart(threadWorker.DoWork));
aThread.Start();
timerCheckWorker.Enabled = true;
}
private void PrintOut(string sStr)
{
string sTemp = txtFeedback.Text;
txtFeedback.Text = sStr + "\r\n" + sTemp;
}
private void timerCheckWorker_Tick(object sender, EventArgs e)
{
if (aThread.IsAlive)
{
PrintOut("Thread Running");
}
else
{
PrintOut("Thread Done");
timerCheckWorker.Enabled = false;
}
}
}
}
Here is the CMySimpleThreadClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace SimpleThreadExample
{
class CMySimpleThreadClass
{
// This method that will be called when the thread is started
public void DoWork()
{
StreamWriter sw = new StreamWriter("C:\\Test.csv");
int iRowCount = 10000;
int iColCount = 100;
for (int iRow = 0; iRow < iRowCount; iRow++)
{
string sLine = "";
for (int iCol = 0; iCol < iColCount; iCol++)
{
sLine = sLine + iRow.ToString() + "_" + iCol.ToString();
if (iCol < iColCount - 1) sLine = sLine + ",";
}
sw.WriteLine(sLine);
}
sw.Close();
System.Diagnostics.Process.Start("C:\\Test.csv");
}
}
}
Go ahead and run the program and hit the "Run" button on the form. While the
thread is working grab the window and drag it around. Notice the Program keeps
working and remains responsive even while writing the file.
Congratulations you can now write threaded programs.