/// <summary> /// 从连接池中取出一个可用连接对象 /// </summary> /// <returns></returns> cf c public static HClient GetHclient() { m_mutex.WaitOne(); //先阻塞 for (var i = 0; i < clientlist.Count; i++) { if (clientlist[i].IsFree) { if (!clientlist[i].IsOpen) { clientlist[i].Open(); } clientlist[i].IsFree = false; m_mutex.ReleaseMutex();//释放资源 return(clientlist[i]); } } if (clientlist.Count > MAX_CLIENTCOUNT) { throw new Exception("超出最大HClinet最大个数"); } var item = new HClient(); item.Open(); item.IsFree = false; m_mutex.ReleaseMutex();//释放资源 return(item); }
/// <summary> /// 从连接池中取出一个可用连接对象 /// </summary> /// <returns></returns> cf c public static HClient GetHclient() { try { m_mutex.WaitOne(); //先阻塞 for (var i = 0; i < clientlist.Count; i++) { if (clientlist[i].IsFree) { clientlist[i].IsFree = false; //m_mutex.ReleaseMutex();//释放资源 return clientlist[i]; } } if (clientlist.Count > MAX_CLIENTCOUNT) throw new Exception("超出最大HClinet最大个数"); var item = new HClient(); item.Open(); item.IsFree = false; //m_mutex.ReleaseMutex();//释放资源 return item; } finally { m_mutex.ReleaseMutex();//释放资源 } }