Thread usage in C,

Source: Internet
Author: User

Thread usage in C,

Create a thread by Delegate

 

Static int sum (int x)

{

Return x + x;

{

Func <int> a = sum;

Create a new thread format: a. BeginInvoke (10 );

Obtain the return value of a thread: IAsyncResult ar = a. BeginInvoke (10, null, null );

// Method 2 IAsyncResult ar = a. BeginInvoke (10, OnCallback, );

// Method 3: Use the lanmba expression, for example:

/* IAsyncResult ar = */a. BeginInvoke (10, ar =>

{

Int res = a. EndInvoke (ar );

Console. WriteLine (res );

}, Null)

The first null parameter represents the callback function, for example:

Static void OnCallback (IAsyncResult ar)

{

The second null parameter is the data passed by the callback function. For example:

Func <int> a = ar. AsyncState as Func <int>; // converts the ar. AsyncState type to a Func <int> type object.

}

Obtain the status of the thread return value: the return value of ar. IsCompleted is true, indicating that the thread has ended, and false indicates that the thread has not ended.

Get the execution result of the thread: int res = a. EndInvoke (ar );

 

Use Thread to create a Thread

Create Thread: Thread t = new Thread (Function); // Function is the Function to be executed by starting a new Thread.

Start thread: t. Start (); // if the Function has parameters (which must be of the Object type), the Function parameters are transmitted by the Start Function.

Get Thread ID: Thread. CurrentThread. ManagedThreadId

 

You can also write a class by yourself, for example:

Class MyThread

{

Private string fileName;

Private string filePath;

Public MyThread (string filename, string filepath)

{

This. fileName = filename;

This. filePath = filepath;

}

Public void DownFile (string filename, string filepath)

{

Console. WriteLine ("downloading:" + filepath + filename );

......

Console. WriteLine ("download completed ");

}

}

Call in program

MyThread myt = new MyThread ("filename", "filepath ");

Thread t = new Thread (myt. Down );

T. Start ();

Set the background thread method, for example, t. IsBackground = true;

Threads are divided into foreground threads and background threads. Background threads are forcibly terminated with the end of foreground threads.

Force terminate a thread: t. Abort ();

Thread with prior execution: t. Join (); // pause the current program. Execute t. Join () and then execute the thread that is not completed.

 

Thread Pool in C #

A thread pool ThreadPool has been encapsulated in C #. By default, 1023 worker threads and 1000 I/O threads are encapsulated under dual-core conditions. All threads are background threads

Method for calling the thread pool: ThreadPool. QueueUserWorkItem (Function); // Function is a custom Function. This Function must have a parameter of the object type.

Usage like locking a running thread: lock (lock Object );

 

 

Related Article

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.