Microsoft Knowledge Base Email Alertz

KBAlertz.com: (815804) - In Visual C# .NET, you can write multithreaded applications. This article describes how a simple Visual C# .NET application can create and manage threads. back to the top Requirements The following list outlines the recommended hardware, software,...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 2.0 Web Hosting with SQL 2005: Click Here!
Discount ASP.NET Hosting


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




How to create a thread by using Visual C#

Article ID:815804
Last Review:August 20, 2007
Revision:2.10
On This Page

SUMMARY

You 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 top

Requirements

The 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

1.Start Microsoft Visual Studio .NET, Microsoft Visual Studio 2005, or Microsoft Visual C# 2005 Express Edition.
2.Create a new Visual C# Windows Application project named ThreadWinApp.
3.Add a Button control to the form. By default, the button is named Button1.
4.Add a ProgressBar component to the form. By default, the progress bar is named ProgressBar1.
5.Right-click the form, and then click View Code.
6.Add the following statement to the beginning of the file:
using System.Threading;
7.Add the following Click event handler for Button1:
private void button1_Click(object sender, System.EventArgs e)
{
	MessageBox.Show("This is the main thread");
}
8.Add the following variable to the Form1 class:
private Thread trd;
9.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.
10.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 top

Verify that it works

1.Build and run the application. Notice that the value in ProgressBar1 changes randomly. This is the new thread in operation.
2.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 top

Troubleshoot

In 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 top

REFERENCES

For more information, visit the following Microsoft Web site or the .NET Framework SDK Documentation:
"Thread Class"
http://msdn2.microsoft.com/en-us/library/system.threading.thread(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.threading.thread(vs.71).aspx)

Back to the top


APPLIES TO
•Microsoft Visual C# .NET 2002 Standard Edition
•Microsoft Visual C# .NET 2003 Standard Edition
•Microsoft ADO.NET 1.0
•Microsoft Common Language Runtime (included with the .NET Framework) 1.0
•Microsoft Visual C# 2005 Express Edition

Back to the top

Keywords: 
kbnamespace kbthreadsync kbthread kbhowtomaster KB815804

Back to the top

       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Anonymous User Report As Irrelevant  
Written: 4/15/2007 8:42 AM
This example is broken. A control shall only be accessed by the thread that created it. In this example, the auxiliar thread is updating the value of the progress bar directly, which is just wrong. See the documentation of Control.Invoke or Control.BeginInvoke for correcting this.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please