int GetServerTime(bool bChangeEnableState, out string strError) { strError = ""; LibraryChannel channel = this.GetChannel(); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在获得服务器当前时钟 ..."); stop.BeginLoop(); if (bChangeEnableState == true) { this.EnableControls(false); } // int value = Interlocked.Increment(ref this.m_nIn); try { #if NO if (value > 1) { return(0); // 防止重入 } #endif string strTime = ""; long lRet = channel.GetClock( stop, out strTime, out strError); if (lRet == -1) { return(-1); } // 已经采用带有时区的 RFC1123 字符串显示 this.RFC1123TimeString = strTime; return(0); } finally { // Interlocked.Decrement(ref this.m_nIn); if (bChangeEnableState == true) { this.EnableControls(true); } stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); this.ReturnChannel(channel); } }
// 探测和 dp2library 服务器的通讯是否正常 // return.Value // -1 本函数执行出现异常 // 0 网络不正常 // 1 网络正常 public static NormalResult DetectLibraryNetwork() { LibraryChannel channel = App.CurrentApp.GetChannel(); var old_timeout = channel.Timeout; channel.Timeout = TimeSpan.FromSeconds(5); // 设置 5 秒超时,避免等待太久 try { int nRedoCount = 0; REDO: long lRet = channel.GetClock(null, out string _, out string strError); if (lRet == -1) { // 一次重试机会 if (lRet == -1 && (channel.ErrorCode == ErrorCode.RequestCanceled || channel.ErrorCode == ErrorCode.RequestError) && nRedoCount < 2) { nRedoCount++; goto REDO; } return(new NormalResult { Value = 0, ErrorInfo = strError }); } return(new NormalResult { Value = 1 }); } catch (Exception ex) { return(new NormalResult { Value = -1, ErrorInfo = $"DetectNetwork() 出现异常:{ex.Message}" }); } finally { channel.Timeout = old_timeout; App.CurrentApp.ReturnChannel(channel); } }