I am trying to make a timer that refreshes every 5 seconds. Here is my code:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Timers;

namespace rpanel_server
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

        Timer MyTimer = new Timer();
        MyTimer.Interval = (4000);
        MyTimer.Tick += new EventHandler(MyTimer_Tick);
        MyTimer.Start();
        }

        private PerformanceCounter theMemCounter = new PerformanceCounter("Memory", "Available MBytes");

        // Main stuff
        public object getCPUCounter()
        {
            PerformanceCounter cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";

            // will always start at 0
            dynamic firstValue = cpuCounter.NextValue();
            System.Threading.Thread.Sleep(1000);
            // now matches task manager reading
            dynamic secondValue = cpuCounter.NextValue();

            return secondValue;
        }

        void MainFormLoad(object sender, EventArgs e)
        {
            label8.Text = getCPUCounter() + "%";
            label9.Text = theMemCounter.NextValue().ToString();
        }

        private void MyTimer_Tick(object sender, EventArgs e)
        {
            label8.Text = getCPUCounter() + "%";
            label9.Text = theMemCounter.NextValue().ToString();
        }
    }
}

When I run that code, I get this:
'Timer' is an ambiguous reference between 'System.Windows.Forms.Timer' and 'System.Timers.Timer' (CS0104)

No matter what timer I make, it will still throw that error. I don't know what is wrong with this code.

The .NET framework has three Timer classes. One is in System.Windows.Forms, one is in System.Timers, and one is in System.Threading. When you include more than one namespace with the same class name as a meber, the type must be fully qualified to resolve the ambiguity:

var MyTimer = new System.Timers.Timer();

Now I get this:
'System.Timers.Timer' does not contain a definition for 'Tick' and no extension method 'Tick' accepting a first argument of type 'System.Timers.Timer' could be found (are you missing a using directive or an assembly reference?) (CS1061)

Apologies, I assumed you wanted the System.Timers timer. But it looks like you're trying to use the System.Windows.Forms timer:

var MyTimer = new System.Windows.Forms.Timer();

This may be of use to you in chosing which to utilize.

This seems to freeze the GUI, is there a way to use the timer without freezing the GUI?

The WinForms timer only runs in the UI thread. If you want to run in another thread, use another timer. I'd recommend System.Timers.Timer.

I used System.Timers.Timer in my code but nothing happens:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Timers;
using System.Threading.Tasks;

namespace rpanel_server
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent(); 
        }

        private PerformanceCounter theMemCounter = new PerformanceCounter("Memory", "Available MBytes");

        // Main stuff
        public object getCPUCounter()
        {
            PerformanceCounter cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";

            // will always start at 0
            dynamic firstValue = cpuCounter.NextValue();
            System.Threading.Thread.Sleep(1000);
            // now matches task manager reading
            dynamic secondValue = cpuCounter.NextValue();

            return secondValue;
        }

        void MainFormLoad(object sender, EventArgs e)
        {
            label8.Text = getCPUCounter() + "%";
            label9.Text = theMemCounter.NextValue().ToString();

            // Timer
            System.Timers.Timer timer = new System.Timers.Timer(2000);

            // Hook up the Elapsed event for the timer.
            timer.Elapsed += new ElapsedEventHandler(MyTimer_Tick);

            timer.Enabled = true;

            timer.Start();
        }

        private void MyTimer_Tick(object source, ElapsedEventArgs e)
        {
            label8.Text = getCPUCounter() + "%";
            label9.Text = theMemCounter.NextValue().ToString();
        }
        }
    }

No errors or warnings are in the console.

In your first code remove using System.Timers;
System.Timers contains a Timer to be used in a multithreaded environment. Just use the Forms Timer in your case. Hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.