J.C.Morris
J.C.Morris

Reputation: 803

Timer/Refresh functionality for text box

In my application, I have two text boxes accompanied with two labels: "Connected" and "Not Connected". As seen in my code, if a connection is established, the "Connected" text box will fill with Green, indicating a network connection. It will be red if not.

The functionality of connection detection is working just fine, however, I have to re-open the application for it to detect the change. I am looking for a way to refresh the application every 5-10 seconds or so automatically to detect any change in connectivity. I don't want to flush out the contents of any other field or box, just the color text boxes. A soft polling loop so to speak. How would I go about doing this using the Timer method. Should I create a new thread in which to run the timer and refresh the box?

Thanks.

  if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() == false)
        {
            noConnect.Select();  //if not connected, turn box red
            noConnect.BackColor = Color.Red;

        }

        else
        {
            netConnect.Select();  // if connected, turn box green
            netConnect.BackColor = Color.Lime;

        }

        //need to refresh box/application without losing other box/field contents 
        //in order to constantly check connectivity around 5-10 seconds or so
        //constantly check connectivity 

Upvotes: 1

Views: 16365

Answers (3)

parapura rajkumar
parapura rajkumar

Reputation: 24403

Something like this would work

    public Form1()
    {
        InitializeComponent();
        var timer = new Timer();
        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = 10000; //10 seconds
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        if (your_function_call())
        {
            netConnect.BackColor = Color.Green;
        }
        else
            netConnect.BackColor = Color.Red;
    }

The timer_Tick would be repeatedly called every interval and you can poll your status and update controls. Because the timer call back is called in the UI-thread you can update any UI elements.

From Timer Class

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread. When you use this timer, use the Tick event to perform a polling operation or to display a splash screen for a specified period of time. Whenever the Enabled property is set to true and the Interval property is greater than zero, the Tick event is raised at intervals based on the Interval property setting.

This solution uses System.Windows.Forms.Timer that calls the tick on UI-thread. If you use System.Timers.Timer the callback won't be on UI-thread.

Upvotes: 6

Maarten
Maarten

Reputation: 22945

You can create a timer somewhere in your application

var timer = new System.Timers.Timer();
timer.Interval = 5000; // every 5 seconds
timer.Elapsed = (s, e) => {
    // Your code
};
timer.Start();

Note: please be aware that your code in the Elapsed event handler can/will run on another thread!

Upvotes: 1

elyashiv
elyashiv

Reputation: 3691

just create the timer. it well run on his own thread without you doing any thing else.

Upvotes: 1

Related Questions