Monitoring internet connection at runtime

Asked

Viewed 814 times

2

I’m using this method in C# to be able to identify whether or not the computer is connected to the internet

    private bool VerificarConexao()
    {
        if (NetworkInterface.GetIsNetworkAvailable()) {
            return true;
        }
        else{
            return false;
        }
    }

The method only runs when it is called, but I wanted it to run automatically (every 30 seconds) as if it were in the background. What is the best way to solve this problem ?

  • Why do you need this? What kind of application will you use? It’s probably a mistake to do this. Even if you do, just use return NetworkInterface.GetIsNetworkAvailable()

  • Why a mistake do this? I want to know if my internet connection is active or not through my system ...

  • This you have already written, if you can answer what I asked, I can help more.

  • 2

    I answered what you asked in the comment above, did not understand ?

  • Observing: NetworkInterface.GetIsNetworkAvailable() does not check if the computer is connected to the internet and yes if it has any network connection available to connect. To check if there is a connection, I suggest using the System.Net.NetworkInformation.Ping to achieve a specific field.

2 answers

7


For this purpose you can use the Fluentscheduler...

using FluentScheduler;

public class MyRegistry : Registry
{
    public MyRegistry()
    {
       //executa o código a cada 30 segundos
       Schedule<SeuMetodo>().ToRunNow().AndEvery(30).Seconds();
    }

} 

2

It has several forms a simple would be like this:

using static System.Console;
using System.Timers;

public class Program {
    public static void Main() {
        var aTimer = new Timer();
        aTimer.Elapsed += new ElapsedEventHandler(OnTimed);
        aTimer.Interval = 30000;
        aTimer.Enabled = true;
        ReadLine();
    }

    private static void OnTimed(object source, ElapsedEventArgs e) {
        if (NetworkInterface.GetIsNetworkAvailable()) {
            //faz alguma coisa aqui
        } else {
            //pode fazer algo se a rede caiu
        }
    }
}

I put in the Github for future reference.

Note that you cannot return whether you are active or not. Will you return to whom? Who called right? Who called was the Timer. He doesn’t know what to do with that return, he can’t do anything. So the right thing is to do something in there.

Actually there is little or no gain in doing this. Why do you need to keep checking if the connection is active? If it really makes sense to do this, it is right to take an action in the method itself that is called every 30 seconds. If in fact what you need is to know if the connection is active before doing any operation, then it is best to keep the function you had created and call it when you need it. Or better still try to do what you want, if the network fails, you treat it and avoid a running condition.

private bool VerificarConexao() {
    return NetworkInterface.GetIsNetworkAvailable();
}

It doesn’t check the internet, it checks the network.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.