Tread basics in C #-Part One

Source: Internet
Author: User
1. Thread classification in. NET Framework (CLR [Common Language Runtime] thread Support)

I. system. Threading. thread;

Ii. system. Diagnostics. processthread (present an operating system thread)

2. a simpe sample creating a thread.

Using ...*

Using system. Threading;

Namespace mythread

{

Class threadtest

{

Public void showtest ()

{

Console. writeline ("this is a thread test ");

}

Public static void main ()

{

// Declare a delegate instance for threadstart delegate and initialize it's method;

Threadstart Ts = new threadstart (New mythread (). showtest );
// Declare a thread and intial it with a delegate.

Thread mythread = new thread (TS );
// Start the thread.

Mythread. Start ();
}

}

}


If everyting goes well, you will see such window:



3. multi-thread upgrade ligy.

We have 5 senarios:

I. Delegate invoke asychronousely.

Ii. Creating yourself threads openly.

Iii. Timer.

Iv. Inner asychronous support.

V. Order the projects into thread-pool.


4. Delegate asychronousely

In. NET Framework, the most popular model to implementate asychronouse method is providing two respective method: Beginxxx () AndEndxxx (). Accordingly, there are two relative methods which to excute operation sychrounousely.

Invoking beginxxx () can start the operation. after invoking this method, it will return at once, while this method will be executed in the thread-Pool still. when the return result is needed, we can invoke endxxx (). in case of asychrounouse operation has finished when invoking this endxxx (), It wll return the result of asychrounouse operation. in another case of operation running still. endxxx () will wait until the asychrounouse operation finished and return the result.

This design model involves multi classes to implemtate it, that is:

A). system. Io. filestream (beginread ()/endread (); beginwrite ()/endwrite (), to read and write Stream)

B). system. net. webrequest (beginrequest ()/endrequest (), to request a remote server)

C). system. messaging. messagequeue (beginreceive ()/endreceive (), to receive a message)

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.