简体   繁体   中英

Check internet connection is available or not

I am trying to detect the internet connection, if the internet connection is available and it is connected, it will continue, otherwise it will throw message box says that the connection is not available.

What I am encounter is whether the internet connection is connected or not connected, the code will continue.

Here is the code:

** The program will continue to worker_ProgressChanged , even though there is no internet connection available **

public CheckUpdates()
        {
            InitializeComponent();

            bool checkConnection = CheckConnection.IsConnectedToInternet();

            progressBar1.Style = ProgressBarStyle.Marquee;

            if (checkConnection == true)
            {
                backgroundWorker1.WorkerReportsProgress = true;
                backgroundWorker1.DoWork += new DoWorkEventHandler(worker_DoWork);
                backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
                backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            }

            else
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                _sound.Play();

                DialogResult _dialogResult = MessageBox.Show("No connection available, please check your internet connection!", "No connection");

                if (_dialogResult == DialogResult.OK)
                {
                    this.Hide();

                    this.Close();
                }
            }

        }

        private void CheckUpdates_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;

            if (e.ProgressPercentage.Equals(100))
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                _sound.Play();

                DialogResult _dialogResult = MessageBox.Show("No updates were available!", "No updates");

                if (_dialogResult == DialogResult.OK)
                {
                    this.Hide();

                    this.Close();
                }
            }
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _timer.Enabled = true;
            _timer.Tick += new EventHandler(Timer_Tick);
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 100; i++)
            {
                backgroundWorker1.ReportProgress(i);
                System.Threading.Thread.Sleep(100);
            }
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            _timer.Enabled = false;
        }

class CheckConnection
    {
        [DllImport("wininet.dll")]

        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

        public static bool IsConnectedToInternet()
        {
            int Desc;
            return InternetGetConnectedState(out Desc, 0);
        }

    }

Your answer much appreciated!

Thank you very much!

Because you call backgroundWorker1.RunWorkerAsync(); in the event WelcomeScreen_Load or in the event CheckUpdates_Load without checking if the Internet is connected or not. (Probably the worker_ProgressChanged is defined at design time withing the backgroundWorkder properties)

These events doesn't seems to be related to a Form.Load event handler because your class doesn't derive from Form. However it seems clear that you need to put a check there otherwise whoever triggers these events will start your background worker.

    private void CheckUpdates_Load(object sender, EventArgs e)
    {
        if(CheckConnection.IsConnectedToInternet())
             backgroundWorker1.RunWorkerAsync();
    }

    private void WelcomeScreen_Load(object sender, EventArgs e)
    {
        if(CheckConnection.IsConnectedToInternet())
             backgroundWorker1.RunWorkerAsync();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM