0

I'm coding a c# console app that connects to vpn and after it's connected it sends an http request to a web server. The problem is that I can't know the exact time when vpn is connected. Because of this the app sends the http request before connected to vpn and this results in timed out for vpn, then it takes a much longer time to recover.

        Process.Start("vpn-connect.bat");
        Thread.Sleep(5000);
        //Send https request

vpn-connect.bat starts the vpn connection but it doesn't return anything to tell me if connection succeeded or not. So I have to rely on Thread.Sleep to wait before sending http request and is a bad solution cause it's failing very often.

Is there any way to replace Thread.Sleep with something that's accurate and it tells the app when vpn is finally connected?

G3n1t0
  • 198
  • 1
  • 10
  • maybe this is [related](https://stackoverflow.com/questions/12227540/how-can-i-know-whether-a-vpn-connection-is-established-or-not) – jeb Jul 27 '23 at 15:45
  • @jeb I tried every solution there but none of them worked, the vpn adapter is up and running as soon as connection start which is wrong. – G3n1t0 Jul 27 '23 at 17:21
  • maybe you can send a request to some site thats only available in vpn. that would be a workaround. – jeb Jul 28 '23 at 05:53
  • @jeb I tried sending api request to test the connection but the problem is when I send an request while trying to connect to vpn, the vpn results in timeout, then it takes 1 minute to reconnect. – G3n1t0 Jul 28 '23 at 12:49

1 Answers1

0

You could use Process.WaitForExit() method to wait for the batch file to finish. Problem is that it does block the thread.

If you wish to not block the thread you can use Exited event.

See:

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit?view=net-7.0

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.exited?view=net-7.0#system-diagnostics-process-exited

Santiago
  • 124
  • 9
  • As soon as the connection starts the batch file exits, not waiting for the connection to be establilished. – G3n1t0 Jul 27 '23 at 17:20