public class DeleOrLamada
{
public void ThreadMessage(string data)
{
string message = string.Format("ThreadName is {0} Thread ID is {1}", data, Thread.CurrentThread.ManagedThreadId);
Console.WriteLine(message);
}
{
public void ThreadMessage(string data)
{
string message = string.Format("ThreadName is {0} Thread ID is {1}", data, Thread.CurrentThread.ManagedThreadId);
Console.WriteLine(message);
}
//模拟执行长时间任务
public void Sleep(object state)
{
string name = (string)state;
ThreadMessage(name);
Console.WriteLine("我还没有执行完呢,请耐心等待....!!!");
Thread.Sleep(3000);
}
//这里的参数不为object,你可以定义任何类型,任何参数
public void ShowMessage(string message)
{
ThreadMessage(message);
Console.WriteLine("我还没有执行完呢,请耐心等待....");
Thread.Sleep(3000);
}
public void Sleep(object state)
{
string name = (string)state;
ThreadMessage(name);
Console.WriteLine("我还没有执行完呢,请耐心等待....!!!");
Thread.Sleep(3000);
}
//这里的参数不为object,你可以定义任何类型,任何参数
public void ShowMessage(string message)
{
ThreadMessage(message);
Console.WriteLine("我还没有执行完呢,请耐心等待....");
Thread.Sleep(3000);
}
}
class Program
{
static void Main(string[] args)
{
DeleOrLamada deleOrLamada = new DeleOrLamada();
Thread thread = new Thread(new ThreadStart(()=>{ deleOrLamada.ShowMessage("ShowMessage1"); })); //又重新定义了一个函数,只是匿名了而已
//thread.Start();
// ParameterizedThreadStart parameterizedThreadStart = new ParameterizedThreadStart(deleOrLamada.Sleep); //直接绑定到Sleep函数
// Thread thread1 = new Thread(parameterizedThreadStart);
//注意这两种方法!!!!
Thread thread1 = new Thread(new ParameterizedThreadStart((object a) => { deleOrLamada.Sleep("ShowMessage2"); }));
thread1.Start();
thread.Start();
Console.ReadKey();
}
}
{
static void Main(string[] args)
{
DeleOrLamada deleOrLamada = new DeleOrLamada();
Thread thread = new Thread(new ThreadStart(()=>{ deleOrLamada.ShowMessage("ShowMessage1"); })); //又重新定义了一个函数,只是匿名了而已
//thread.Start();
// ParameterizedThreadStart parameterizedThreadStart = new ParameterizedThreadStart(deleOrLamada.Sleep); //直接绑定到Sleep函数
// Thread thread1 = new Thread(parameterizedThreadStart);
//注意这两种方法!!!!
Thread thread1 = new Thread(new ParameterizedThreadStart((object a) => { deleOrLamada.Sleep("ShowMessage2"); }));
thread1.Start();
thread.Start();
Console.ReadKey();
}
}