2011年3月11日 星期五

[C#] Multi-thread delete file sample

just boring..

using
System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;

namespace WPFTest
{
class Range
{
int m_start;
int m_end;
public Range(int start,int end)
{
m_start = start;
m_end = end;
}
public int Start
{
get { return m_start; }
set { m_start = value; }
}
public int End
{
get { return m_end; }
set { m_end = value; }
}
}

class Orz
{
List<string> m_delNameList;
List<Range> m_RangeList;
int m_threadCount;
Orz(){}
public Orz(List<string> delList,int threadCount)
{
m_delNameList = delList;
m_threadCount = threadCount;
m_RangeList = new List<Range>();
}
public void Start()
{
int count = m_delNameList.Count / m_threadCount;
for(int i = 0; i < m_threadCount ; i++)
{
Thread t = new Thread(new ThreadStart(DeleteProc));
t.Name = i.ToString();
m_RangeList.Add(new Range(i * count, (i + 1) * count));
t.Start();
}
}

void DeleteProc()
{
int index = Convert.ToInt32(Thread.CurrentThread.Name);
Range range = m_RangeList[index];
for (int i = range.Start; i < (index == m_threadCount-1 ? m_delNameList.Count : range.End); i++)
{
File.Delete(m_delNameList[i]);
}
}
}
}

沒有留言:

張貼留言

Hello