Create a multi-threaded Application

Source: Internet
Author: User

As I have been working on a silver-electric networking project some time ago, I have used multithreading and some basic technologies based on the underlying socket communication. I will take a look at it and put it here. The basic programming points and steps of socket have been posted above. Here we will continue to post the basic knowledge of multithreading.

Define namespace
In. net, The multithreading function is defined in the system. Threading namespace.
Using System. threading;

Start thread
The thread class in the system. Threading namespace represents a thread object. This class object can be used to create new threads, delete, pause, and recover threads.

The followingCodeUse the Thread class to create a new thread and then start the thread:

ThreadMythread;

Mythread = new thread (New threadstart (writedata ));
Mythread. Start ();

Writedata is a function to be executed by this thread. The Code is as follows:
Protected void writedata (){
String STR;
For (INT I = 0; I <= 10000; I ++ ){
STR = "secondary thread" + I. tostring ();
Console. writeline (listview1.listitems. Count, STR, 0, new string [] {""});
Update ();
}
}

Kill thread
The abort method of the thread class is used to permanently kill a thread. However, before calling the abort method, you must determine whether the thread is still activated ,:
If (mythread. isalive)
{
Mythread. Abort ();
}

Pause a thread
The thread. Sleep method is used to pause a thread for a period of time. The Code is as follows:
Mythread. Sleep (INT );

Set thread priority
We can use the threadpriority attribute of the thread class to set the thread priority. The value range of thread priority is normal, abovenormal, belownormal, highest, or lowest. See the following configuration code:

Mythread. Priority = threadpriority. Highest;

Delay thread
The suspend method of the thread class can delay a thread (suspend a thread ). The thread is delayed until the resume method is called.

If (Mythread. Threadstate = threadstate. Running)
{
Mythread. Suspend ();
}

Restore delayed threads
Call the resume method to restore a delayed thread. If the thread is not delayed, the resume method is invalid.
If (Mythread. Threadstate = threadstate. Suspended)
{
Mythread. Resume ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.