110,533
社区成员
发帖
与我相关
我的任务
分享
private Samephore pool;
Action a;
int min, max;
void ThreadPoolInit(int min, int max)
{
this.min = min;
this.max = max;
pool = new Samephore(0, max);
for (int i = 0; i <= min; ++i)
{
Thread t = new Thread(p);
t.IsBackground = true;
t.Start();
}
}
void p()
{
while(true)
{
pool.WaitOne();
Action t;
if (a != null)
{
lock (a)
{
if (a != null)
{
t = a;
}
}
}
if (t != null) t();
int num = pool.Release();
if (num > min) break;
}
}
void QueueWork(Action d)
{
//省略了max检查及新线程创建
lock (a)
{
a = d;
}
pool.Release();
}
private bool BackupSite()
{
successnum = 0;
if (UploadTrace.IsTaskCancel(block))
{
return false;
}
try
{
for (int i = 0; i < file.Length; i++)
{
doBackup t2 = new doBackup(this.BackSiteThread);
t2.BeginInvoke(i, new AsyncCallback(doBackupCallBack), t2);
}
}
catch (Exception ex)
{
return false;
}
while (true)
{
if (hasError || successnum == SiteObj.FilePairList.Count)
return hasError ? false : true;
System.Threading.Thread.Sleep(100);
}
}
private delegate void doBackup(int threadid);
private void doBackupCallBack(IAsyncResult IAR)
{
doBackup t2 = (doBackup)(IAR.AsyncState);
t2.EndInvoke(IAR);
t2.Clone();
}