构造方法传参:
可以定义专门的类,在New的时候,在构造函数中传递参数,从而实现线程中带参数传递。
源代码;
专门的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadPro
{
public class MyThread
{
private string _FilePath;
private string _FileName;
public MyThread(string filePath, string fileName)
{
_FileName = fileName;
_FilePath = filePath;
}
public void DownFile()
{
Console.WriteLine("开始下载: " + _FilePath + "/" + _FileName);
Thread.Sleep(5000);
Console.WriteLine("下载完毕");
}
}
主程序:
/***
*构造方法传参
*
*
*
*
*
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadPro
{
class Program
{
/// <summary>
/// 线程传递多个参数
/// </summary>
public void Test1()
{
MyThread myThread = new MyThread(@"", "abc.mov");
Thread t1 = new Thread(myThread.DownFile);
t1.Start();
}
static void Main(string[] args)
{
Program obj = new Program();
obj.Test1();
Console.ReadKey();
}
}
}
}