先定一个类,既有构造函数又有类方法:
public class WriteNumber
{
/// <summary>
/// 构造函数
/// </summary>
public WriteNumber()
{
Console.WriteLine("WriteNumber-----begin");
Console.WriteLine("Name:" + Thread.CurrentThread.Name);
Console.WriteLine("WriteNumber-----end");
} /// <summary>
/// 类方法
/// </summary>
public void WriteString()
{
Console.WriteLine("WriteString-----begin");
Console.WriteLine("Name:" + Thread.CurrentThread.Name);
Console.WriteLine("WriteString-----end");
}
}
用多线程运行:
public static void Main(string[] args)
{
try
{
Thread.CurrentThread.Name = "main"; //定义当前线程名称
WriteNumber writenumber = new WriteNumber();
Thread t1 = new Thread(new ThreadStart(writenumber.WriteString));
t1.Name = ""; //定义t1线程名称
t1.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} Console.ReadLine();
}
得出的结果: