We can pass a lambda expression to a thread:
using System;
using System.Threading;
class ThreadTest
{
static void Main()
{
Thread t = new Thread(() => Print("Hello from t!"));
t.Start();
}
static void Print(string message) {
Console.WriteLine(message);
}
}
The output:
Hello from t!
Another technique is to pass an argument into Thread's Start method:
using System;
using System.Threading;
class ThreadTest
{
static void Main()
{
Thread t = new Thread(Print);
t.Start("Hello from t!");
}
static void Print(object messageObj)
{
string message = (string)messageObj; // We need to cast here
Console.WriteLine(message);
}
}
The output:
Hello from t!
Thread's constructor is overloaded to accept either of two delegates:
public delegate void ThreadStart();
public delegate void ParameterizedThreadStart (object obj);
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |