C # Thread lecture series (2): Application of Thread class

Source: Internet
Author: User
Tags instance method thread thread class

The basic usage of thread class

The System.Threading.Thread class allows you to start a new thread and run static or instance methods in the thread stack. A delegate (ThreadStart) that has no parameters and does not return a value (return void) can be passed through the construction method of the thread class, which is defined as follows:

[ComVisibleAttribute(true)]
public delegate void ThreadStart()

We can build and run a thread in the following ways.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
Namespace Mythread
{
Class Program
{
public static void Mystaticthreadmethod ()
{
Console.WriteLine ("Mystaticthreadmethod");
}
static void Main (string[] args)
{
Thread thread1 = new Thread (Mystaticthreadmethod);
Thread1. Start (); The thread will run only if you use the Start method
}
}
}

In addition to running a static method, you can run an instance method in a thread, as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
Namespace Mythread
{
Class Program
{
public void Mythreadmethod ()
{
Console.WriteLine ("Mythreadmethod");
}
static void Main (string[] args)
{
Thread thread2 = new Thread (new program (). Mythreadmethod);
Thread2. Start ();
}
}
}

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.