-2

How can I do this in a WinForms app? I've tried it with a System.Windows.Forms.Timer, but when i minimize the application, I can't maximize it again. It lags up the app. I'm using the .Interval property as 500.

EDIT: Code:

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.Timer Loop = new System.Windows.Forms.Timer();
            Loop.Interval = 500;
            Loop.Tick += new EventHandler(UpdateUI);
            Loop.Start();
        }

        void UpdateUI(object sender, EventArgs e)
        {
            //ADD TO LIST ON USER INTERFACE
        }
    }
}
abcd3fg
  • 3
  • 1
  • 5
  • show some code then you will get some help – Jehof Feb 14 '13 at 21:20
  • Code please - we can't be guessing at what you are doing. There are too many ways to do what you describe - without code we can't give an answer. – Oded Feb 14 '13 at 21:20
  • Please show your code. – mbeckish Feb 14 '13 at 21:20
  • 1
    Why are you using an interval property of 500 if you want it to loop every *50* milliseconds? – Jon Skeet Feb 14 '13 at 21:21
  • In Java you use Thread.sleep(500), but if you run it in the GUI thread... the whole application will hang. – le3th4x0rbot Feb 14 '13 at 21:21
  • .NET is not a real-time framework. You can specify 500ms in many different ways, but you have 2 problems. Windows is not a real-time operating system, and .NET is not a real-time VM. You will never get he kind of reliable, repeatable precision you are looking for out of the Windows + .NET combination. – Sam Axe Feb 14 '13 at 21:23
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Feb 14 '13 at 21:24

1 Answers1

0

The Timer class specifies Interval as milliseconds. So your value of 500 indicates an interval of half a second. Specify 50 instead.

Also, you are not guaranteed to have the timer fire exactly each 50 milliseconds. Only somewhere around 50 milliseconds.

You should keep a reference to your timer object.

public partial class Form1 : Form
{
    Timer loop;

    public Form1()
    {
        InitializeComponent();
        this.loop = new System.Windows.Forms.Timer();
        loop.Interval = 50;
        loop.Tick += new EventHandler(UpdateUI);
        loop.Start();
    }

    void UpdateUI(object sender, EventArgs e)
    {
        //ADD TO LIST ON USER INTERFACE
    }
}
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157