How to: Create a thread using Visual C # .NET (from MSDN)

xiaoxiao2021-03-06  62

For Microsoft Visual Basic .NET versions of this article, see

315577.

This article refers to the following Microsoft .NET Framework Class library namespace:

System.threading

This task content

summary

Create a Visual C # .NET application using threads to confirm that it can use troubleshooting reference

summary

In Visual C # .NET, you can write multithreaded applications. This article describes how simple Visual C # .NET applications create and manage threads.

Back to top

Require the following list summarizes the recommended hardware, software, network structure, and the required service pack:

Microsoft Windows 2000 or Microsoft Windows XP Microsoft Visual C # .NET This article assumes that you are familiar with the following topics:

Visual C # .NET Programming Visual Studio .NET Integration Development Environment (IDE)

Back to top

Create a Visual C # .NET application using threads

Start Microsoft Visual Studio .NET. The new Visual C # .NET Windows application project called Threadwinapp. Add a "Button" control to the form. By default, the button is called "Button1". Add a "ProgressBar" component to the form. By default, the schedule is called "ProgressBar1". Right-click the form and click View Code. Add the following statement to the beginning of the file: use system.threading; add the following "Click" event handler to "Button1": Private Void Button1_Click (Object Sender, System.EventArgs E)

{

MessageBox.show ("this is the main trread");

} Add the following variables to the Form1 class: private thread trd;

Add below to 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

Newval = this.progressbar1.minimum;

THIS.PROGRESSBAR1.VALUE = NewVal;

Thread.sleep (100);

}

} Note: This is the basic code for creating a thread. This code is an unlimited loop, which is randomly incremented or reduced in "progressbar1", then wait 100 milliseconds to continue again. Add the following load event handler for "Form1". This code will create a new thread to make the thread a background thread and then start 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 top

Confirm that it can be used

Generate and run the app. Please note that the values ​​in "Progressbar1" will be randomly changed. This is a new thread works. To demonstrate the thread of the main thread independent of the "Progressbar1" value, click the button on the form. A dialog will appear, which shows the following error message: this is the main threadwait for Input. Please note that the values ​​in "Progressbar1" will continue to change.

Back to top

Troubleshooting In more complex applications, make sure that multiple threads are synchronized when accessing a shared variable. For more information, see the "Lock" statement and related topics in the Visual C # .NET online help documentation.

Back to top

Refer to more information, visit the following Microsoft Web site or see the .NET Framework SDK document:

"Thread class" http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemthreadingthreadclasstopic.asp

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

Recent Updated: 2003-9-15 (1.3) Keywords: KBNamespace Kbthreadsync KBTHREAD KBHOWTOMASTER KB815804 KBAUDDEVEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-109039.html

New Post(0)