检查互联网连接是否可用

时间:2014-04-14 14:24:36

标签: c# winforms connection

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

我遇到的是互联网连接是连接还是未连接,代码将继续。

以下是代码:

**即使没有可用的互联网连接,该计划仍将继续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);
        }

    }

你的回答非常感谢!

非常感谢!

1 个答案:

答案 0 :(得分:0)

因为您在事件backgroundWorker1.RunWorkerAsync();或事件WelcomeScreen_Load中致电CheckUpdates_Load而未检查互联网是否已连接。 (可能在设计时使用backgroundWorkder属性定义worker_ProgressChanged

这些事件似乎与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();
    }