System.dll中出现未处理的“System.Threading.SemaphoreFullException”类型异常 附加信息:将指定的计数添加到信号量将导致它超过其最大计数。
点击按钮
ThreadPool.SetMaxThreads(MAX_THREADS, MAX_THREADS);
semaphore = new System.Threading.Semaphore(MAX_THREADS, MAX_THREADS);
mainDownloadThread = new Thread(() => startDownload(list));
mainDownloadThread.SetApartmentState(ApartmentState.STA);
mainDownloadThread.Start();
主线程在这里
void startDownload(List<string> list)
{
manualResetEvent = new ManualResetEvent(false);
foreach (string link in list)
{
ThreadInfo threadInfo = new ThreadInfo();
threadInfo.Index = threadIndex++;
threadInfo.link = link;
threadInfo.ManualResetEvent = manualResetEvent;
ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessRequest), threadInfo);
totalItems++;
}
manualResetEvent.WaitOne();
base.BeginInvoke(new frmMain.EnableExportDelegate(this.enableInterface), null);
}
和processRequest
void processRequest(object obj)
{
try
{
ThreadInfo threadInfo = obj as ThreadInfo;
semaphore.WaitOne();
manualResetEvent = threadInfo.ManualResetEvent;
string link = threadInfo.link;
if (isCancelled == true) return;
if (manualResetEvent.WaitOne(0, false)) return;
//stuff
.
.
.
.
if (Interlocked.Decrement(ref this.totalItems) == 0 || isCancelled == true)
{
manualResetEvent.Set();
}
}
catch
{
}
finally
{
semaphore.Release();
//////Exception thrown on above line//////
}
}
不确定我是做对还是错,但是在点击上面的按钮后,它会抛出异常。