Auto reconnect to Internet after drop out

Bytec

New member
Joined
Jan 24, 2014
Messages
1
Programming Experience
Beginner
Hi all,

I am trying to write a windows form app that requires an Internet connection. I have done most of the work apart from one area of which I do not understand or know how to do.

The app requires that it always has an Internet connection, if the Internet drops for whatever reason I want the app to keep retrying until the connection is restored. I have read that I could use "try, catch" but this is where the little skill I have fails me.

Can anyone help.

The code I have so far: I am using www.google.co.uk for test this.

Form1.Designer.cs
C#:
namespace Browser2014
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.webBrowser1 = new System.Windows.Forms.WebBrowser();
            this.SuspendLayout();
            // 
            // webBrowser1
            // 
            this.webBrowser1.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
            this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.webBrowser1.IsWebBrowserContextMenuEnabled = false;
            this.webBrowser1.Location = new System.Drawing.Point(0, 0);
            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser1.Name = "webBrowser1";
            this.webBrowser1.ScrollBarsEnabled = false;
            this.webBrowser1.Size = new System.Drawing.Size(1012, 843);
            this.webBrowser1.TabIndex = 0;
            this.webBrowser1.Url = new System.Uri("[URL="http://www.google.co.uk"]Google[/URL]", System.UriKind.Absolute);
            this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.BackColor = System.Drawing.Color.Black;
            this.ClientSize = new System.Drawing.Size(1012, 843);
            this.ControlBox = false;
            this.Controls.Add(this.webBrowser1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.ResumeLayout(false);
        }
        #endregion
        private System.Windows.Forms.WebBrowser webBrowser1;
    }
}

Form1.cs
C#:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
 
namespace NaveXrowser2014
{
    public partial class Form1 : Form
    {
        public static bool CheckForInternetConnection()
        {
            try
            {
                using (var navexClient = new WebClient())
                using (var webConnection = navexClient.OpenRead("[URL="http://www.google.com"]Google[/URL]"))
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
        }
    }
}
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] 
 

[/SIZE][/FONT][/SIZE][/FONT]

Many thanks for any help you can give.

Dereck
 
Think about it. If you want to do something a number of times then how do you usually achieve it? You use a loop, right? There are a number of different types of loops so which one do you use? In this case, you don't know how many times you'll want to loop. Which type(s) of loop is good at that? What you want to do is keep trying your code as long as you keep catching an exception. How would you implement that in code using a loop.

There are other considerations too. Think about what is going to produce the best user experience in terms of functionality and then work out the code to implement that functionality. For instance, do you want to just try again immediately or do you want a bit of a delay? Do you want to keep trying indefinitely or give up after a few failures? What sort of feedback do you want to provide to the user during this process?
 

Latest posts

Back
Top Bottom