using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using HuizhongLibrary.Data; using HuizhongLibrary.Log; namespace HuizhongLibrary { public class DatabaseConnect { private AutoResetEvent AutoReset = new AutoResetEvent(false); bool IsRun = false; public event Action ConnectComplete; #region 启动检测 public void Start() { Thread t = new Thread(this.ThreadWhile); t.Start(); } #endregion #region 停止检测 public void Stop() { IsRun = false; AutoReset.Set(); } #endregion #region 测试连接 void ThreadWhile() { IsRun = true; while (IsRun) { Database db = DatabaseFactory.CreateDatabase(); bool bk=db.TestConnection(); if (bk == true) { IsRun = false; if (this.ConnectComplete != null) this.ConnectComplete(); return; } ErrorFollow.TraceWrite("数据库连接失败","暂停30秒后再试", db.ConnectionString); AutoReset.WaitOne(30000); } } #endregion } }