Refresh the form every second

66,426

Solution 1

Use a Timer control, it's UI thread invoked and a control available to you via the Form Designer.

private void Form1_Load(object sender, EventArgs e)
{
    Timer timer = new Timer();
    timer.Interval = (10 * 1000); // 10 secs
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}

private void timer_Tick(object sender, EventArgs e)
{
   //refresh here...
}

Solution 2

You can use System.Windows.Forms.Timer Control to refresh your form controls.

Try This:

System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=5000;//5 seconds
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();

private void timer1_Tick(object sender, EventArgs e)
{
     //do whatever you want 
     RefreshControls();
}

Solution 3

And this is the way I would usually handle it.

Scenario: Some Thread should update something every n-seconds, but we want to be able to trigger an update before that time runs out without risking to block anything.

    private static readonly TimeSpan timeout = TimeSpan.FromSeconds(10);

    private static readonly object Synchronizer = new { };

    public static void idle()
    {
        //make sure, it is only called from the Thread that updates.
        AssertThread.Name("MyUpdateThreadName");
        lock (Synchronizer) {
            Monitor.Wait(Synchronizer, timeout);
        }
    }

    public static void nudge()
    {
        //anyone can call this
        lock (Synchronizer) {
            Monitor.PulseAll(Synchronizer);
        }
    }

Here, idle() would be called from a Thread that does the update, and nudge() could be called from anywhere to start an update before the timeout expires.

It works very reliable even when a lot of nudges come in very fast, due to the complex interaction between Wait and Pulse - considering the locked state of the Synchronizer. This also has the neat side effect of never dead(b)locking the update thread.

Of course, if the update involves GUI stuff, you would have to take the whole InvokeRequired tour. That means in the while loop of the update thread it looks something like

        if (MyForm.InvokeRequired)
            MyForm.BeginInvoke(sendinfo, message);
        else
            sendinfo(message);

with sendinfo probably being some kind of Action or Delegate. And you would never, ever, ever call idle() directly from the GUI thread!

The only Not-So-Obvious behaviour I could find so far: If you have a lot of nudges very fast and the Thread is actually updating a GUI, there is a huge difference between Invoke() and BeginInvoke(). Depending on what you want, Invoke actually gives the GUI time to react to interactions and BeginInvoke can quickly flood the message pump and make the GUI unresposive (which in my case is (weirdly enough) the the wanted behaviour :-) ).

Share:
66,426
GoGo
Author by

GoGo

Updated on October 05, 2020

Comments

  • GoGo
    GoGo over 3 years

    I have a C# Windows form that has a couple of text boxes and buttons. Also it has a datagrid view that shows a sql table. I create a refresh button that allow me to refresh the table so I can see the updated items inside the table. I was wondering is there any way to refresh the table by it self. Like every 10 second.Or instead of table, perhaps the entire form loaded or refreshed by itself every 10 second?