public async Task<bool> VPNDesconectouAsync() { bool pingable = false; Ping pinger = null; int qtdPacotesPerdidos = 0; try { pinger = new Ping(); for (int i = 0; i < QtdDisparos; i++) { PingReply reply = pinger.Send(NomeOuIP); pingable = reply.Status == IPStatus.Success; if (!pingable) { qtdPacotesPerdidos++; } await Task.Delay(IntervaloEntrePings); } } catch (PingException) { this.eStatusVPN = StatusVPN.VPNDesconectada; return true; } finally { if (pinger != null) { pinger.Dispose(); } } if (qtdPacotesPerdidos >= QtdPerdaPacotesDefineVPNDesconectada) { this.eStatusVPN = StatusVPN.VPNDesconectada; return true; } else { this.eStatusVPN = StatusVPN.VPNConectada; return false; } }
public bool Desconectar() { try { p = new Process(); p.StartInfo = new ProcessStartInfo("rasdial.exe", $"{NomeDaConexao} /DISCONNECT"); p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.Start(); this.StatusDaVPN = StatusVPN.VPNDesconectada; return true; } catch(System.Exception) { this.StatusDaVPN = StatusVPN.VPNStatusNaoReconhecido; return false; } }