Search code examples
c#memorycheat-engine

C# program crashing when checking checkbox


I coded a cheat for a game but I want to create a menu and I did it like this. If the checkbox is checked do while (checkbox.checked). So I started testing my program and due some reasons my program is crashing when I am checking the checkbox. Can anybody help me?

namespace Hack
{
    public partial class Form1 : Form
    {
        public Form1()

        {
            int PBase = 0x00509B74;
            int Health = 0xf8;
            int mgammo = 0x150;

            VAMemory vam = new VAMemory("ac_client");
            int localplayer = vam.ReadInt32((IntPtr)PBase);
            {

            }

            InitializeComponent();


        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
                while (checkBox1.Checked)
            {
            int PBase = 0x00509B74;
            int Health = 0xf8;

            VAMemory vam = new VAMemory("ac_client");
            int localplayer = vam.ReadInt32((IntPtr)PBase);
            {
                int address = localplayer + Health;
                vam.WriteInt32((IntPtr)address, 1337);
            }
                Thread.Sleep(200);
            }
        }
    }
}

IzzyMichiel


Solution

  • Your code has a deadlock in the checkBox1_CheckedChanged event handler. The UI thread gets trapped in the event handler, so it can't service any other events. When that happens, Windows will eventually put up a "This program has stopped responding" message.

    As with many other deadlocks, the solution is simply to do the processing on a new thread. The easiest way to handle this is to spawn a thread pool thread to do the processing work for you.

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            ThreadPool.QueueUserWorkItem((state) =>
            {
                while (checkBox1.Checked)
                {
                    // ...
                }
            });
        }
    }