Multithreading in C #-Thread class

Source: Internet
Author: User

Now C # is recommended to discard the use of suspend, resume pause/resume the thread, and use the abort method to interrupt a thread as little as possible.

We recommend that you use the following Thread Synchronization Methods: , , ,

Public thread (threadstart start );

The other is with parameters (parameterizedthreadstart delegate )--

Public thread (parameterizedthreadstart start );

Parameterizedthreadstart:

Public Delegate void parameterizedthreadstart (Object OBJ );

Example:

1. Without parameters:

// Define the thread method:

Private Static void threadmain ()
{
Console. writeline ("this is other thread main method .");
}

// Call:

Thread mythread = new thread (threadmain );
Mythread. Start ();

2. With parameters:

// Define the thread method:

Private Static void mainthreadwithparameters (Object O)
{
Data d = (data) O; // type conversion
Console. writeline ("running in a thread, received {0}", D. Message );
}

Public struct data
{
Public String message;
}

// Call:

VaR DATA = new data {message = "info "};
Thread t2 = new thread (Mainthreadwithparameters);
T2.start (Data);// Input parameters

3. PASS Parameters by defining classes:

// Define the classes for storing data and thread methods:

Public class mythread
{
Private string message;
Public mythread (string data)
{
This. Message = data;
}
Public void threadmethod ()
{
Console. writeline ("running in a thread, data: {0 }",This. Message);
}
}

// Call

var OBJ = new mythread ("info");
thread mythread = new thread ( obj. threadmethod ); // threadstart DeleGate
mythread. start ();

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.