Michael Bulacan
Michael Bulacan

Reputation: 17

How to Optimize while(true), or where to apply the waithandle event for Less CPU usage?

This code uses alot of CPU since its infinite loop, How can I stop it from looping when just idle and only activates when pressing left and right mouse button. I read about waithandle event but I dont know where to put it in my code. Tried and failed.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test
{
    public static class Anti //does something
    {
        public static void Run()
        {
            var R1 = new Random(Guid.NewGuid().GetHashCode());

            while (true)
            {
                try
                {
                    if (Main.CEnabled)
                    {
                        if (InputHandler.IsKeyDown(Keys.LButton) && InputHandler.IsKeyDown(Keys.RButton))
                        {
                            switch (Main.CurrentWeapon.Mode)
                            {
                                case FiringMode.Automatic:
                                    if (InputHandler.IsKeyDown(Keys.LButton) && InputHandler.IsKeyDown(Keys.RButton))
                                    {

                                    }
                                    else if (InputHandler.IsKeyDown(Keys.LButton) && InputHandler.IsKeyDown(Keys.RButton) && InputHandler.IsKeyDown((Keys)162))
                                    {
                                        Console.WriteLine("Success");
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
                catch { }
            }
        }
    }
}

Upvotes: 0

Views: 112

Answers (0)

Related Questions