1

Starting an app in Visual Studio 2019. It is a Windows Forms app using .NET Core 5. I have used System.Timers.Timer previously and found it to be of low jitter. However, I am not able to find it in Visual Studio toolbox. There is a timer under All Windows Forms it is: System.Windows.Forms.Timer There is another timer under Components, it is also System.Windows.Forms.Timer. This timer is not reliable. The numbers of ticks vary quite a bit from what is specified. Fallback is to construct a System.Timers.Timer by hand. Looking at my previous app though it came from the toolbox. Any suggestions are appreciated!

HBC531
  • 47
  • 6
  • Are you trying to run speed tests? Have you ever used Stopwatch? It's convenient for speed tests. – Andrew Reese Jun 17 '21 at 20:45
  • 1
    I don't think `System.Timers.Timer` was ever in the WinForm's Designer Toolbox. – LarsTech Jun 17 '21 at 20:45
  • Andrew Reese: have a state machine in the tick handler. I need about 20 ticks in a minute. LarsTech: here is code parts from the previous app: (in designer.cs) ((System.ComponentModel.ISupportInitialize)(this.timerSys)).BeginInit(); in: #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// I am afraid the build will kick my manual inserts out. – HBC531 Jun 17 '21 at 21:08
  • You can always add a System.Timers.Timer object in the designer code behind, outside of the region `InitializeComponent` method. – Timothy G. Jun 17 '21 at 23:49
  • @LarsTech https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/february/comparing-the-timer-classes-in-the-net-framework-class-library says it used to be (i don't remember it either) – Caius Jard Jun 18 '21 at 06:41

1 Answers1

-2

As others said, System.Timers.Timer can not be found in the Toolbox.

Because it doesn't inherit from System.Windows.Forms.Control class. It is not a windows form control.

You can refer to the following code to know how to use it in winforms.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        System.Timers.Timer timer;
        private void button1_Click(object sender, EventArgs e)
        {
            timer.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer = new System.Timers.Timer(1000);
            timer.Elapsed += Timer_Elapsed;

        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            richTextBox1.AppendText("Hello, world"+Environment.NewLine);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer.Stop();
        }
    }

Result:

enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • 1
    Please provide some justification for turning off cross thread checking rather than adopting one of the Microsft advocated routes of accessing controls in a not-the-thread-that-created-it scenario (e.g. Invoke or setting SyncronizingObject property) – Caius Jard Jun 18 '21 at 06:40
  • Thank you. I was able to place relevant code in te form1 constructor. It is working fine. I still think/recall at some point it was possible to pull it out of the toolbox/components. – HBC531 Jun 19 '21 at 22:04
  • @HBC531, I have explained that why it can not be found in toolbox. Please check it again. I think there is no affect if the timer can be pull out of the toolbox when you use it. – Jack J Jun Jun 21 '21 at 05:46