전체 구현을 작성하는 것이 복잡한 점입니다. 포인터를 줄 수 있습니다. 인터넷 연결을 사용할 수 있는지 여부를 모니터링하는 타이머가 필요합니다. 나는 당신의 필요에 따라 완전한 부분 논리를 작성했다. 개인 변수를 유지하여 연결 링크 연결/다시 연결을 모니터링해야 할 수도 있습니다.
public class Example
{
/// <summary>
/// External method for checking internet access:
/// </summary>
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
/// <summary>
/// C# callable method to check internet access
/// </summary>
public static bool IsConnectedToInternet()
{
int Description;
return InternetGetConnectedState(out Description, 0);
}
private static Timer aTimer;
/// <summary>
/// Fired when a connection is made to the server
/// </summary>
public event EventHandler ConnectedToNetwork;
public void OnConnected()
{
try
{
if (null != ConnectedToNetwork)
{
ConnectedToNetwork(this, new EventArgs());
}
}
catch (Exception ex)
{
}
}
/// <summary>
/// Fired when loses its connection to the server
/// </summary>
public event EventHandler DisconnectedFromNetwork;
/// <summary>
/// Clears server notifications
/// </summary>
public void OnDisconnected()
{
try
{
if (null != DisconnectedFromNetwork)
{
DisconnectedFromNetwork(this, new EventArgs());
}
}
catch (Exception ex)
{
}
}
public void Intialize()
{
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(2000); //Timer will start when your application is started
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program... ");
Console.ReadLine();
Console.WriteLine("Terminating the application...");
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
if (IsConnectedToInternet())
{
OnConnected();
//Code to APP will wake up and execute SQL commands.
}
else
{
OnDisconnected();
}
}
}
// DBClass 'ClientCodeSubscription' should verify whetehr the DB is up and running.
class DBClass
{
public bool DBServerIsRunning { get; set; }
//In DB area
private void ClientCodeSubscription()
{
if (DBServerIsRunning) //Check to see if DB service is running.
{
Example ex = new Example();
ex.ConnectedToNetwork += Example_ConnectedToNetwork; //this is your client code probably in this case the DB
}
}
static void Example_ConnectedToNetwork(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
[so]에 오신 것을 환영합니다. 일반적으로 SQL Server dB와 통신하고 나중에 연결을 닫으려면 트랜잭션 또는 트랜잭션 일괄 처리를 실행할 때만 연결을 엽니 다. 그래서'Connection.Open()'이 예외를 일으켰다면 당신은 잠들고 예외를 발생시키지 않을 때까지 주기적으로 연결을 ping 할 수 있습니다. 너가 알고 싶었던거야? –
@Jeremy Thompson : 당신은 이미 말했습니다 : "Connection.Open()이 예외를 발생 시키면 잠자기 상태가되어 예외가 발생하지 않을 때까지 주기적으로 연결을 ping 할 수 있습니다."그러나 내 요구는 내 앱이 잠자기 상태가 될 것입니다. (CPU 자원을 소비하지 않음) 연결이 가능할 때까지 기다려야합니다 (네트워크 케이블이 플러그 또는 다른 이유입니다). 깨어나서 작동합니다. – GIANGPZO
- 답장 5 개월 만에 꽤 예 .. SQL 서비스 시작 (이벤트 로그를 통해)을 통보하지 않으면 알림이 표시되지 않습니다. AFAIK는 유지하지 않는 한 방법이 없습니다. 핑/폴링. db 연결 가용성을위한 'FolderWatcher'라이브러리가 있다면 좋을 것입니다. 미안해. 하나도 몰라. –