/// <summary> /// Shutdown the worker threads gracefully /// </summary> /// <param name="wait">Set this to true if you want to wait for threads to shutdown.</param> public void Shutdown(bool wait) { InternalLogger.LogInformation(String.Format("BoundedThreadPool \"{0}\" Shutting Down", this.name)); if (this.status != ThreadPoolStatus.Active) { return; } Thread[] threads; lock (taskQueue) { // Signal the threads to exit this.status = ThreadPoolStatus.ShuttingDown; Monitor.PulseAll(taskQueue); // Make a copy of the threads' references in the pool threads = new Thread[numWorkerThreads]; workerThreads.CopyTo(threads, 0); } if (wait) { for (int i = 0; i < this.numWorkerThreads; i++) { threads[i].Join(); } } // Abort the threads in the pool foreach (Thread thread in threads) { if ((thread != null) && thread.IsAlive) { try { thread.Abort(); // Shutdown } catch (SecurityException ex) { ex.GetHashCode(); } catch (ThreadStateException ex) { ex.GetHashCode(); // In case the thread has been terminated // after the check if it is alive. } } } // clear threads refs Array.Clear(workerThreads, 0, workerThreads.Length); this.status = ThreadPoolStatus.Closed; InternalLogger.LogInformation(String.Format("BoundedThreadPool \"{0}\" Shut Down", this.name)); }
/// <summary> /// 写文件的回调函数 /// </summary> /// <param name="result"></param> /// <returns></returns> private void CallbackWrite(IAsyncResult result) { //线程池现状;这里的线程必须要休眠200毫秒.否则是出现文件已关闭错误情形 Thread.Sleep(200); status = ThreadPoolStatus.CallbackWrite; //结束异步写入 streamWrite = (FileStream)result.AsyncState; streamWrite.EndWrite(result); streamWrite.Close(); }
/// <summary> /// 获取实例AsyncReadWrite /// </summary> /// <returns></returns> public static AsyncReadWrite GetInstance() { if (instance == null) { ThreadPool.SetMaxThreads(Config.ThreadPoolSize, Config.ThreadPoolSize); //设置线程池最大值 status = ThreadPoolStatus.Start; //线程现状状态 instance = new AsyncReadWrite(); return(instance); } else { return(instance); } }
/// <summary> /// 读取回调函数 /// </summary> /// <param name="result"></param> /// <returns></returns> private void CallbackRead(IAsyncResult result) { status = ThreadPoolStatus.CallbackRead; FileData fileData = (FileData)result.AsyncState; int length = fileData.Stream.EndRead(result); fileData.Stream.Close(); //读取到的长度不一致 if (length != fileData.Length) { Log.WriteLog(new Error(ThreadPoolMessage(), "读取流没有完成!(读取到的长度不一致)")); } else { string data = Encoding.UTF8.GetString(fileData.ByteData, 0, fileData.Length); fileData.StringData = data; readData = fileData; } }
/// <summary> /// Initialize and kick off the required number of worker threads /// </summary> private void InitWorkerThreads() { // TODO: This should be locked so someone cannot initialize a threadpool while it is shutting down workerThreads = new Thread[numWorkerThreads]; for (int i = 0; i < numWorkerThreads; i++) { Thread thread = new Thread(WorkerLoop); thread.Name = string.IsNullOrEmpty(this.name) ? this.name : this.name + " #" + (i + 1); // "Pooled threads are always background threads." // http://stackoverflow.com/questions/3646901/thread-pool-and-isbackground-in-net thread.IsBackground = true; workerThreads[i] = thread; workerThreads[i].Start(); } this.status = ThreadPoolStatus.Active; }