How to create a thread by using Visual C# (815804)
The information in this article applies to:
- Microsoft Visual C# .NET (2002)
- Microsoft Visual C# .NET (2003)
- Microsoft ADO.NET (included with the .NET Framework) 1.0
- Microsoft Common Language Runtime (included with the .NET Framework) 1.0
- Microsoft Visual C# 2005, Express Edition
For a Microsoft Visual Basic .NET version of this
article, see
315577. This article refers to the
following Microsoft .NET Framework Class Library namespaces:
IN THIS TASKSUMMARYYou can write multithreaded
applications in Microsoft Visual C# .NET or in Microsoft Visual C#. This article describes how a simple Visual C# application
can create and manage threads. back to
the topRequirementsThe following list outlines the recommended hardware, software,
network infrastructure, and service packs that you need:
- Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
- Microsoft Visual C# .NET or Microsoft Visual C# 2005
This article assumes that you are familiar with the following
topics:
- Visual C# programming
- Visual Studio .NET Integrated Development Environment
(IDE) or Visual Studio 2005 IDE
back to the
top Create a Visual C# application with threads - Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- Create a new Visual C# Windows Application project
named ThreadWinApp.
- Add a Button control to the form. By
default, the button is named Button1.
- Add a ProgressBar component to the form.
By default, the progress bar is named ProgressBar1.
- Right-click the form, and then click View
Code.
- Add the following statement to the beginning of the file:
using System.Threading; - Add the following Click event handler for
Button1:
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("This is the main thread");
} - Add the following variable to the Form1 class:
private Thread trd;
- Add the following method to the Form1 class:
private void ThreadTask()
{
int stp;
int newval;
Random rnd=new Random();
while(true)
{
stp=this.progressBar1.Step*rnd.Next(-1,2);
newval = this.progressBar1.Value + stp;
if (newval > this.progressBar1.Maximum)
newval = this.progressBar1.Maximum;
else if (newval < this.progressBar1.Minimum)
newval = this.progressBar1.Minimum;
this.progressBar1.Value = newval;
Thread.Sleep(100);
}
} Note This is the code that underlies the thread. This code is an
infinite loop that randomly increments or decrements the value in
ProgressBar1, and then waits 100 milliseconds before it
continues. - Add the following Load event handler for Form1. This code creates a new
thread, makes the thread a background thread, and then starts the thread.
private void Form1_Load(object sender, System.EventArgs e)
{
Thread trd = new Thread(new ThreadStart(this.ThreadTask));
trd.IsBackground = true;
trd.Start();
} back to the
topVerify that it works- Build and run the application. Notice that the value in
ProgressBar1 changes randomly. This is the new thread in
operation.
- To demonstrate that the main thread is independent of the
thread that changes the value of ProgressBar1, click the
button on the form. You receive a dialog box with the following error message:
This is the main thread
Wait for input. Notice that the value in
ProgressBar1 continues to change. back to the topTroubleshootIn more complex applications, make sure that you synchronize
multiple threads when you access shared variables. For more information, see
the lock statement and related topics in the Visual C# .NET
Online Help documentation. back to the
topREFERENCESFor more information, visit the following Microsoft Web site
or the .NET Framework SDK Documentation:
Modification Type: | Major | Last Reviewed: | 1/21/2006 |
---|
Keywords: | kbNameSpace kbThreadSync kbThread kbHOWTOmaster KB815804 kbAudDeveloper |
---|
|