C # Multithreading Implementation method

Source: Internet
Author: User
Tags thread class

The classes that implement threading application development in C # are included in the System.Threading namespace, and the Thread,threadpool class is more commonly used

The thread class constructor functions as a thread, with two overloaded versions, one with parameters and one without parameters:

 Public Thread (ThreadStart start);  Public Thread (Parameterizedthreadstart start);

The declarations of ThreadStart or Parameterizedthreadstart types are as follows:

 Public Delegate void ThreadStart ();  Public Delegate void Parameterizedthreadstart (Object obj);

The steps to create a thread are as follows:

//create a thread without parameters (the function that the thread is associated with has no parameters)ThreadStart Mythreadstart=NewThreadStart (myFunction);//Pass the function that the thread is going to start (delegate? ) into the ThreadStart variable to goThread mythread=NewThread (MyThread);//Creating ThreadsMythread.start ();//Start Thread//create a thread with parametersParameterrizedthreadstart Mythreadstart=NewParameterrizedthreadstart (myFunction);//Pass the function that the thread is going to start (delegate? ) into the ThreadStart variable to goThread MyThread=NewThread (MyThread);//Creating ThreadsMythread.start (obj);//Start Thread

Precautions:

The method that the thread is associated with must be a void return type

Creates a thread that associates a method with a parameter, the type of the argument must be of type object, and can have only one parameter

The associated method is either static or instantiated

C # Multithreading Implementation method

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.