using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BackupAndImport
{
///
/// 封装ManualResetEvent
///
public class MutipleThreadResetEvent : IDisposable
{
private readonly ManualResetEvent done;
private readonly int total;
private long current;
///
/// 构造函数
///
/// 需要等待执行的线程总数
public MutipleThreadResetEvent(int total)
{
this.total = total;
current = total;
done = new ManualResetEvent(false);
}
///
/// 唤醒一个等待的线程
///
public void SetOne()
{
// Interlocked 原子操作类 ,此处将计数器减1
if (Interlocked.Decrement(ref current) == 0)
{
//当所以等待线程执行完毕时,唤醒等待的线程
done.Set();
}
}
///
/// 等待所以线程执行完毕
///
public void WaitAll()
{
done.WaitOne();
}
///
/// 释放对象占用的空间
///
public void Dispose()
{
((IDisposable)done).Dispose();
}
}
}