C # thread (1)

Source: Internet
Author: User

C # And. net base classes are used to develop multi-threaded applicationsProgramProvides powerful support. Below are the notes I have taken from reading books and combining some online resources. Because the thread-related knowledge is complicated and profound (and lacks development experience), it is easy to write or even naive, and it is inevitable to understand what is wrong.
1. How to Create a thread (common creation method)

Code
Using System;
Using System. collections;
Using System. Collections. Generic;
Using System. Threading;

Namespace Threadstudy
{
Public   Class Mythreadclass
{
Public   Static   Void Threadtest ()
{
Console. writeline ( " This is a thread test. The worker thread is started! " );
}
Public   Static   Void Threadtestwithparameter ( Object Stateinfo)
{
Console. writeline ( String . Format ( " This is {0}. The worker thread is started! " , Stateinfo ));
}
Delegate   Void Threadtestdelegate ( Object Objname );

StaticThreadtestdelegate mytest= NewThreadtestdelegate (threadtestwithparameter );

// The callback function after the thread is completed.
Public   Static   Void Taskfinished (iasyncresult result)
{
// Mytest. endinvoke (result ); // No return value
Console. writeline ( " Thread test callback end. " );
}

/* How to Create a thread? */
Public   Static   Void Main ()
{
// 1. Use the Thread class
/* A. No parameter Delegation */
Threadstart TS =   New Threadstart (threadtest ); // The threadstart delegate (with no parameter) is used to tell the sub-thread about the method of execution.
Thread currentthread =   New Thread (TS );
Currentthread. Name =   " My first thread test without Parameter " ; // Name the thread, not required
Currentthread. Start (); // Start a new thread
Currentthread. Abort ();
Thread. Sleep ( 2000 );
/* B. Delegate with Parameters */
Parameterizedthreadstart PTS =   New Parameterizedthreadstart (threadtestwithparameter ); // Use the parameterizedthreadstart delegate (parameter) to tell the sub-thread how to execute
Thread curthread =   New Thread (PTS );
Curthread. Name =   " My first thread test with parameter (s) " ; // Name the thread, not required
Curthread. Start ( " My first thread test with a parameter " ); // Start a new thread and enter or enter one parameter (or multiple parameters)
Curthread. Abort ();
Thread. Sleep ( 2000 );

// 2. Use the threadpool class
Waitcallback WCB =   New Waitcallback (threadtestwithparameter ); // Use the waitcallback delegate to tell the sub-thread what method to execute (either with or without parameters)
Threadpool. queueuserworkitem (WCB, " My first threadpool Test " );
Threadpool. queueuserworkitem (threadtestwithparameter, " My second threadpool Test " );

Thread. Sleep (2000);

// 3. Use delegate. begininvoke
Mytest. begininvoke ( " My thread test without callback " , Null , Null ); // Asynchronous execution starts here. If no subsequent operations are required, callback is not used.

Thread. Sleep ( 2000 );

//Applicable to parameters that need to be passed and return parameters
Mytest. begininvoke ("My thread test with call back",NewAsynccallback (taskfinished ),Null);//Asynchronous execution is started here, and a callback function can be provided.

/* Finally, obtain information about the currently running thread. */
Console. writeline (thread. currentthread. currentculture. tostring ());
Console. writeline (thread. currentthread. currentuiculture. tostring ());
Console. writeline (thread. currentthread. managedthreadid. tostring ());
Console. writeline (thread. currentthread. isthreadpoolthread. tostring ());
Console. writeline (thread. currentthread. isalive. tostring ());
Console. writeline (thread. currentthread. isbackground. tostring ());
Console. writeline (thread. currentthread. Priority. tostring ());

Console. Read ();
}
}
}

2. thread priority
If multiple threads are running in an application, but some threads are more important than other threads, the thread priority is required. Generally, when a thread with a higher priority is working, no time slice is allocated to the thread with a lower priority. High-priority threads can completely stop low-priority thread execution, so be careful when changing the thread priority.
The thread priority can be defined as enumeration threadpriority, that is, highest, abovenormal, normal, belownormal, and lowest.

Code
Using System;
Using System. Threading;

Class Program
{
Static   Int Interval;
Static   Void Main ()
{
Console. writeline ( " Please input a number: " );
Interval =   Int . Parse (console. Readline ());
Thread curthread = Thread. currentthread;
Curthread. Name =   " Main thread " ;

Threadstart TS =   New Threadstart (startmethod );
Thread workerthread =   New Thread (TS );
Workerthread. Name =   " Worker thread " ;
Workerthread. Priority = Threadpriority. abovenormal; // Thread priority
Workerthread. Start ();

Displaynumbers ();
Console. writeline ("Main thread finished!");

Console. Readline ();
}

Static   Void Displaynumbers ()
{
Thread thisthread = Thread. currentthread;
String Name = Thisthread. Name;
Console. writeline ( " Starting thread: "   + Name );
Console. writeline (name +   " : Currentculture = "   + Thisthread. currentculture );
For ( Int I =   0 ; I <   6   * Interval; I ++ )
{
If (I % Interval =   0 )
{
Console. writeline (name +   " : Count has reached "   + I );
}
}
}

Static VoidStartmethod ()
{
Displaynumbers ();
Console. writeline ("Worker thread finished!");
}
}

In the next article, we will introduce the "synchronization" related knowledge about C # threads. Stop here because I am reading a book and haven't digested it yet. ^_^

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.