using System;
using System.Threading;
using System.Threading.Tasks;
namespace 线程_通过Thread类开启
{
class Program
{
static void Downloading(object fileName)
{
Console.WriteLine("startDownloadong"+Thread .CurrentThread .ManagedThreadId +fileName );
Thread.Sleep(2000);
Console.WriteLine("END");
}
static void Main(string[] args)
{
//方式1.直接赋值一个方法引用
/Thread t = new Thread(Downloading);
t.Start();//开始执行线程
Console.WriteLine("Main");/
//开启方式2。赋值一个表达式(匿名方法)
/*Thread t = new Thread(() =>
{
Console.WriteLine("startDownloadong" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(2000);
Console.WriteLine("END");
});
t.Start();*/
/*//方式1.传递参数
Thread t = new Thread(Downloading);
t.Start("XXX");//开始执行线程
Console.WriteLine("Main");*/
//2.创建一个对象,将要传递的数据放到对象里进行传递
Mythread my = new Mythread("wei7", "github.io");
Thread t = new Thread(my.DownloadFile);//以类里的普通方法作为线程方法
t.Start();//开启线程
Console.ReadKey();
}
}
}
//用来传递参数的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace 线程_通过Thread类开启
{
class Mythread
{
private string filename;
private string filepath;
public Mythread(string name,string path)
{
this.filename = name;
this.filepath = path;
}
public void DownloadFile()
{
Console.WriteLine("开始下载"+filepath+filename);
Thread.Sleep(2000);
Console.WriteLine("下载完成");
}
}
}