C# 多线程 类构造函数 类方法之间的关系

先定一个类,既有构造函数又有类方法:

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();
}

得出的结果:

C# 多线程 类构造函数  类方法之间的关系

上一篇:Apache Solr 介绍


下一篇:Mac下finder中无法关闭的小条