Using asp.net to create a thread instance tutorial-practical tips

Source: Internet
Author: User
Tags thread class

The implementation of creating a thread using ASP.net is actually very simple and can be achieved simply by declaring it and providing it with a method delegate at the starting point of the thread. When creating a new thread, you need to use the thread class, which has a constructor that accepts a ThreadStart delegate or a Parameterizedthreadstart delegate. The delegate wraps the method called by the new thread when the Start method is invoked. After the object of the thread class is created, the threading object already exists and is configured, but the actual thread is not created, and the actual thread is created only after the Start method is invoked.

Asp. NET's Start method is used to make a thread scheduled for execution, and it has two overloaded forms, which are described separately below.

(1) causes the operating system to change the status of the current instance to threadstate.running, as follows.

public void Start ()

(2) The operating system changes the state of the current instance to threadstate.running and selects the object that contains the data to be used by the method that the thread executes. The syntax is as follows.

public void Start (Object parameter)

Parameter: An object that contains the data to be used by the method that the thread executes.

Note: If the thread has terminated, it cannot be restarted by calling the Start method again.

For example, create a console application that customizes a static void type method CreateThread, then creates a new thread in the main method by instantiating the thread class object, and then calls the Start method to start the thread. The specific code is as follows:

static void Main (string[] args)
{thread
  mythread;//declaring threads
//Creating an instance of the thread with the ThreadStart delegate of the thread's starting point
Mythread = New Thread (new ThreadStart (CreateThread));
  Mythread.start ()//boot thread
} public
static void CreateThread ()
{
  console.write ("Create thread");
}

Note: The entry of the thread (CreateThread in this case) takes no arguments.

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.