首页 > 代码库 > 2014.2.24 带参数多线程实例

2014.2.24 带参数多线程实例

定义全局变量

ParameterizedThreadStart ParaStart; //表示带参数的多线程

Thread[] tds = new Thread[10]; //运行时再实例化

static object objLock = new object(); //多线程锁

 

//运行中

ParaStart = new ParameterizedThreadStart(ReadPic);

for (int i = 0; i < 10; i++) //创建10个线程处理照片

{

  tds[i] = new Thread(ParaStart); //初始化每个线程

  tds[i].Start(i); //执行

}

 

 

public void ReadPic(object num)

{

   int cur = (int)num;

 

   for (int i = 0; i < pts[cur].Count; i++)

  {

      string path = pts[cur][i].ToString();

      string name = path.Substring(path.LastIndexOf("\\") + 1);

 

      lock (objLock) //此多线程锁可防止返回的s与调用的guline不一致的情况发生

      {

      byte[] imageData = http://www.mamicode.com/TransPIc(path);

      MemoryStream ms = new MemoryStream(imageData);

      Image img = Image.FromStream(ms);

      imgs.Add(img);

      }

   }

}

2014.2.24 带参数多线程实例