C # Methods for creating thread-band Parameters _c# Tutorial

Source: Internet
Author: User

1, the creation of no parameter thread

Thread thread = new Thread (new ThreadStart (Getpic));
Thread. Start ();
private void ShowMessage ()
{
Console.WriteLine ("Hello World");
}

2, with one parameter of the thread

Using Parameterizedthreadstart, the object that contains the data is passed to the thread when the System.Threading.Thread.Start (System.Object) overload method is invoked.

Note that the passed argument can only be type object, but it is possible to force type conversions.

Thread thread = new Thread (new Parameterizedthreadstart (ShowMessage));
String o = "Hello";
Thread. Start ((object) o);
private static void ShowMessage (Object message)
{
String temp = (string) message;
Console.WriteLine (message);
}

3, with two and more parameters of the thread

At this point, the methods and parameters that the thread executes can be encapsulated inside a class, and by instantiating the class, the method can invoke the property to enjoy passing parameters.

For example, you want to pass in two string variables, and then print the output.

public class ThreadTest
{
private string str1;
private string str2;
Public ThreadTest (String A, string b)
{
str1 = A;
str2 = b;
}
public void ThreadProc ()
{
Console.WriteLine (str1 + str2);
}
}
public class Example {public
static void Main () 
{
threadtest tt = new ThreadTest ("Hello", "World");
Thread thread = new Thread (new ThreadStart (TT. ThreadProc));
Thread. Start ();
}

The above is a small set to introduce C # to create a thread with the parameters of the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.