void Init() { _checkRemoteDeskTimer = new System.Threading.Timer( CheckRemoteDeskpTimerCallback, null, Timeout.Infinite, 2000); sbtnConfirmReceipt.Click += sbtnConfirmReceiptClick; sbtnHangUpOpt.Click += (sender, e) => { if (_hangUp) { _checkRemoteDeskTimer.Change(Timeout.Infinite, 2000); AnyDesk.Stop(); _hangUp = false; _remoteDeskState.Connected = false; _remoteDeskState.RemoteInfo = null; tsslRemoteConnectInfo.Text = "暂停挂机"; tsslConnectStartTimeLabel.Visible = false; tsslConnectStartTime.Text = string.Empty; tsslConnectStartTime.Visible = false; tsslConnectEndTimeLabel.Visible = false; tsslConnectEndTime.Text = string.Empty; tsslConnectEndTime.Visible = false; sbtnHangUpOpt.Text = "开始挂机"; } else { _hangUp = true; tsslRemoteConnectInfo.Text = "等待连接..."; sbtnHangUpOpt.Text = "停止挂机"; } }; _remoteOperationBll.OnRequestRemoteInfo = query => { if (!_hangUp || _remoteDeskState.Connected) { _remoteOperationBll.ResponseRequetRemoteInfo( query.UserId, RemoteOperationState.ToClientBusy, null); return; } _remoteDeskState.Connected = true; Invoke(() => { tsslRemoteConnectInfo.Text = string.Format("[{0}] 请求连接...", query.UserAccount); }); var state = RemoteOperationState.Failed; string remoteId = string.Empty; string remotePassword = string.Empty; try { if (AnyDesk.Start(_anyDeskIstallPath, _anyDeskSourceFile)) { remoteId = AnyDesk.GetId(); remotePassword = _remoteDeskState.RemoteDeskPassword; AnyDesk.SetPassword(remotePassword); state = RemoteOperationState.Successed; } else { state = RemoteOperationState.ToClientRemoteDesktopServiceError; } } catch (Exception ex) { state = RemoteOperationState.ToClientRemoteDesktopServiceError; Error("RequestRemoteInfo - Start AnyDesk Service Failed!", ex); } if (state != RemoteOperationState.Successed) { _remoteOperationBll.ResponseRequetRemoteInfo( query.UserId, state, null); _remoteDeskState.Connected = false; Invoke(() => { tsslRemoteConnectInfo.Text = string.Format("等待连接...", query.UserAccount); }); } else { Invoke(() => { tsslRemoteConnectInfo.Text = string.Format("等待 [{0}] 连接...", query.UserAccount); }); _remoteDeskState.RemoteInfo = query; _remoteDeskState.CheckRemoteDeskStateStartTime = DateTime.Now; _remoteDeskState.TimerState = TimerState.CheckRemoteDeskState; Interlocked.Exchange(ref _remoteDeskpCheckTimerCallbackState, 0); _checkRemoteDeskTimer.Change(1000, 2000); _remoteOperationBll.ResponseRequetRemoteInfo( query.UserId, state, new RemoteClientInfo { RemoteId = remoteId, RemotePassword = remotePassword }); } }; }
void CheckRemoteDeskpTimerCallback(object state) { if (Interlocked.Exchange(ref _remoteDeskpCheckTimerCallbackState, 1) == 1) { return; } try { bool started = AnyDesk.IsServerOpened(); if (_remoteDeskState.TimerState == TimerState.CheckRemoteDeskState) { if (started) { _remoteDeskState.RemoteDeskConnectStartTime = DateTime.Now; _remoteDeskState.TimerState = TimerState.CheckRemoteDeskConnectTime; Invoke(() => { tsslRemoteConnectInfo.Text = string.Format( "[{0}] 远程控制中...", _remoteDeskState.RemoteInfo.UserAccount); tsslConnectStartTimeLabel.Visible = true; tsslConnectStartTime.Text = _remoteDeskState .RemoteDeskConnectStartTime.ToString("yyyy-MM-dd HH:mm:ss"); tsslConnectStartTime.Visible = true; tsslConnectEndTimeLabel.Visible = true; tsslConnectEndTime.Text = _remoteDeskState .RemoteDeskConnectEndTime.ToString("yyyy-MM-dd HH:mm:ss"); tsslConnectEndTime.Visible = true; ShowSubAccountInfo(true); }); return; } if (_remoteDeskState.CheckRemoteDeskStateStartTime.AddMilliseconds( _remoteDeskState.MaxCheckRemoteDeskStateTime) <= DateTime.Now) { _checkRemoteDeskTimer.Change(Timeout.Infinite, 2000); Invoke(() => { try { AnyDesk.CloseServer(); AnyDesk.Stop(); } catch (Exception ex) { Error("CheckRemoteDeskpTimerCallback -- AnyDesk Stop!", ex); } tsslRemoteConnectInfo.Text = "等待连接..."; tsslConnectStartTimeLabel.Visible = false; tsslConnectStartTime.Text = string.Empty; tsslConnectStartTime.Visible = false; tsslConnectEndTimeLabel.Visible = false; tsslConnectEndTime.Text = string.Empty; tsslConnectEndTime.Visible = false; }); _remoteDeskState.Connected = false; _remoteDeskState.RemoteInfo = null; _remoteDeskState.ConfirmReceiptInfo = null; _remoteDeskState.ChangeRemoteDeskPassword(); } return; } if (_remoteDeskState.TimerState == TimerState.CheckRemoteDeskConnectTime) { if (started) { var endTime = _remoteDeskState.RemoteDeskConnectEndTime; if (endTime <= DateTime.Now) { _checkRemoteDeskTimer.Change(Timeout.Infinite, 2000); _remoteDeskState.ChangeRemoteDeskPassword(); Invoke(() => { try { int closeCount = 0; while (AnyDesk.IsServerOpened() && closeCount++ < 10) { AnyDesk.CloseServer(); Thread.Sleep(1000); } AnyDesk.SetPassword(_remoteDeskState.RemoteDeskPassword); } catch (Exception ex) { Error("CheckRemoteDeskpTimerCallback -- AnyDesk CloseServer And SetPassword!", ex); } tsslRemoteConnectInfo.Text = "等待连接..."; tsslConnectStartTimeLabel.Visible = false; tsslConnectStartTime.Text = string.Empty; tsslConnectStartTime.Visible = false; tsslConnectEndTimeLabel.Visible = false; tsslConnectEndTime.Text = string.Empty; tsslConnectEndTime.Visible = false; ShowSubAccountInfo(false); }); _remoteDeskState.Connected = false; _remoteDeskState.RemoteInfo = null; _remoteDeskState.ConfirmReceiptInfo = null; return; } TimeSpan timeSpan = endTime - DateTime.Now; if (timeSpan.TotalMilliseconds <= 1000 * 60 * 5) { Invoke(() => { _notifyIcon.ShowBalloonTip(5000, "挂机端远程连接", string.Format("远程连接还有约 {0}分{1}秒 即将断开,请尽快操作!", timeSpan.Minutes, timeSpan.Seconds), ToolTipIcon.Warning); }); } } else { _checkRemoteDeskTimer.Change(Timeout.Infinite, 2000); _remoteDeskState.ChangeRemoteDeskPassword(); _remoteDeskState.Connected = false; _remoteDeskState.RemoteInfo = null; _remoteDeskState.ConfirmReceiptInfo = null; Invoke(() => { try { AnyDesk.SetPassword(_remoteDeskState.RemoteDeskPassword); } catch (Exception ex) { Error("CheckRemoteDeskpTimerCallback -- AnyDesk SetPassword!", ex); } tsslRemoteConnectInfo.Text = "等待连接..."; tsslConnectStartTimeLabel.Visible = false; tsslConnectStartTime.Text = string.Empty; tsslConnectStartTime.Visible = false; tsslConnectEndTimeLabel.Visible = false; tsslConnectEndTime.Text = string.Empty; tsslConnectEndTime.Visible = false; ShowSubAccountInfo(false); }); return; } } } catch (Exception ex) { Error("CheckRemoteDeskpTimerCallback!", ex); } finally { Interlocked.Exchange(ref _remoteDeskpCheckTimerCallbackState, 0); } }