-1

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 2
    Event handlers need to exit eventually or else other events (such as the unchecking of a checkbox) won't be handled. Your event handler never exits because `checkbox.Checked` will never change. – John Wu Feb 12 '17 at 11:34
  • 2
    Do not show your code as a picture. Please edit your question to replace it by code cut and pasted from the IDE. Once you have, select it and press Ctrl-K to format it as a code block. – Roman Marusyk Feb 12 '17 at 11:36

1 Answers1

0

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)
            {
                // ...
            }
        });
    }
}
theB
  • 6,450
  • 1
  • 28
  • 38