检查互联网连接是否可用

本文关键字:是否 连接 互联网 检查 | 更新日期: 2023-09-27 17:58:26

我正在尝试检测互联网连接,如果互联网连接可用并且已连接,它将继续,否则它将抛出消息框,表示连接不可用。

我遇到的是,无论互联网连接是否连接,代码都会继续。

这是代码:

**即使没有可用的互联网连接,程序也将继续到worker_ProgressChanged。**

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);
        }
    }

非常感谢您的回答!

非常感谢!

检查互联网连接是否可用

因为您在事件WelcomeScreen_Load或事件CheckUpdates_Load中调用backgroundWorker1.RunWorkerAsync();而不检查是否连接了Internet。(可能worker_ProgressChanged是在设计时在后台Workder属性中定义的)

这些事件似乎与Form.Load事件处理程序无关,因为您的类不是从Form派生的。但是,很明显,您需要在那里进行检查,否则触发这些事件的人将启动您的后台工作程序。

    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();
    }