/// <summary> /// 测试数据库连接是否正常 /// </summary> /// <param name="conPath">XML路径文件名</param> /// <param name="strMessage">弹出错误信息,指明是哪个数据连接出错</param> /// <returns></returns> public bool IsConnect(string conPath, string strMessage) { string testsql = string.Empty; DataBaseinfo info = XMLHelper.ReadDataBase(conPath); if (info.databasetype == "0") { testsql = "select 1 from dual"; } else { testsql = "select 1"; } bool istrue = false; //测试链接 bool connetctionStatus = false; bool timeOut = false; TimeoutChecker timeout = new TimeoutChecker( delegate { ISqlMapper mapper = null; try { //获取ISqlMapper尝试连接 mapper = mapperobj.NewMapper(info); DataSet ds = new DataSet(); mapper.OpenConnection(); IDbCommand command = mapper.LocalSession.CreateCommand(CommandType.Text); command.CommandText = testsql; //"SELECT 1 FROM DA_CONFIG"; mapper.LocalSession.CreateDataAdapter(command).Fill(ds); connetctionStatus = true; //ShowMessageHelper.ShowBoxMsg("数据库连接成功!"); istrue = true; } catch { //如果没有超时 if (!timeOut) { connetctionStatus = true; ShowMessageHelper.ShowBoxMsg(string.Format("{0}连接失败,数据库不存在或用户名、密码错误,请检查数据库配置!", strMessage)); } } }, delegate { //如果没有链接上 if (!connetctionStatus) { timeOut = true; ShowMessageHelper.ShowBoxMsg(string.Format("{0}连接超时,请检查数据库配置!", strMessage)); } }); timeout.Wait(5000); return(istrue); }
private static async void Timeout(TimeoutChecker checker, Socket client, int value) { await Task.Run(() => { Thread.Sleep(value); }); if (!checker.IsFinished) { checker.IsClosedByTimeoutMethod = true; client.Close(); } }
public bool IsConnect(DataBaseinfo info, string testsql) { bool istrue = false; //测试链接 bool connetctionStatus = false; bool timeOut = false; TimeoutChecker timeout = new TimeoutChecker( delegate { ISqlMapper mapper = null; try { //获取ISqlMapper尝试连接 mapper = mapperobj.NewMapper(info); DataSet ds = new DataSet(); mapper.OpenConnection(); IDbCommand command = mapper.LocalSession.CreateCommand(CommandType.Text); command.CommandText = testsql; mapper.LocalSession.CreateDataAdapter(command).Fill(ds); connetctionStatus = true; ShowMessageHelper.ShowBoxMsg("数据库连接成功!"); istrue = true; } catch (Exception ex) { WriteLog(ex.ToString()); //如果没有超时 if (!timeOut) { connetctionStatus = true; ShowMessageHelper.ShowBoxMsg(ex.Message + "数据库不存在或用户名、密码错误,请重新输入!"); } } }, delegate { //如果没有链接上 if (!connetctionStatus) { timeOut = true; ShowMessageHelper.ShowBoxMsg("连接超时"); } }); timeout.Wait(5000); return(istrue); }
public static void Receive(Socket client, out byte fCode, out byte[] datas, uint timeoutValue = Acp.TIMEOUT_VALUE) { byte[] buffer = new byte[tempBufferSize]; List <byte> dynamicBuffer = new List <byte>(); int totalSize = 0; var stream = new NetworkStream(client); var timeoutChecker = new TimeoutChecker(); Timeout(timeoutChecker, client, (int)timeoutValue); try { do { totalSize += stream.Read(buffer, 0, buffer.Length); dynamicBuffer.AddRange(buffer); Thread.Sleep(receiveInterval); } while (stream.DataAvailable); timeoutChecker.IsFinished = true; datas = new byte[totalSize - 1]; fCode = dynamicBuffer[0]; dynamicBuffer.CopyTo(1, datas, 0, datas.Length); } catch (Exception e) { if (timeoutChecker.IsClosedByTimeoutMethod) { throw new AcpTimeOutException(); } else { throw e; } } }
public ObjectCache(int period) { this._items = SortedList.Synchronized(new SortedList()); this._checker = new TimeoutChecker(period); }
protected ObjectPool() { this.InitializePool(); this._checker = new TimeoutChecker(); }