0

Long Story Short, The app I am making will Launch a Game. The Start button will then Check to make sure the games EXE is running.. IF it is running, it will run a script to press buttons 1, 2, and 3.. After that it will loop that script, but first checking if the game has not crashed (if it crashed it wont run the loop)

My Issue:

While the loop is running, which is using System.Threading.Thread.Sleep, does not let other functions of the app preform (in this case showing and hiding button, and changing label colors.) The main reason this is important is the button it wont is the Stop button which is suppose to end the looping script.

namespace WindowsFormsApp1

{
    public partial class Form1 : Form
    {

        Timer timer;
        Stopwatch sw;

        public Form1()
        {
            InitializeComponent();
            button4.Visible = false;
            button2.Enabled = false;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            label2.Text = sw.Elapsed.Seconds.ToString() + "seconds";
            Application.DoEvents();
        }

        // ===============================================
        // BUTTON FUNCTIONS
        // ===============================================

        // Launch GAME
        private void button1_Click(object sender, EventArgs e)
        {
            // Launch GAME
            Process.Start(@"C:\Windows\System32\Notepad.exe");
            button2.Enabled = true;
        }

        // START BOT
        private void button2_Click(object sender, EventArgs e)
        {
            status.Text = @"Starting Bot..";
            timer = new Timer();
            timer.Interval = (1000);
            timer.Tick += new EventHandler(timer_Tick);
            sw = new Stopwatch();
            timer.Start();
            sw.Start();
            BotReady(sender, e);

        }

        // PLAYER DIED
        private void button3_Click(object sender, EventArgs e)
        {
            KillBot(sender, e);
            status.Text = @"lol u ded";
        }

        // STOP THE BOT
        private void button4_Click(object sender, EventArgs e)
        {
            KillBot(sender, e);
            status.Text = @"Bot Stopped";
            button2.Visible = true;
            button4.Visible = false;
            button2.Enabled = true;
        }

        // KILL GAME AND BOT (IF IT CRASHED OR SOMETHING)
        private void button5_Click(object sender, EventArgs e)
        {

        }

        // ===============================================
        // OTHER FUNCTIONS
        // ===============================================

        // Target GAME application
        private void TargetAQ(object sender, EventArgs e)
        {
            // this part doesnt work yet.
            // Selection.Application and Tab to it
        }


        // CHECK IF GAME IS STILL RUNNING, KILL BOT IF GAME IS NOT DETECTED
        public void CheckStatus(object sender, EventArgs e)
        {
            Process[] GAME = Process.GetProcessesByName("notepad");
            if (GAME.Length == 0)
                // GAME NOT running
            {
                KillBot(sender, e);
                button2.Enabled = false;
                status.Text = @"GAME is not Running, Bot Stopped.";
                uGAME.ForeColor = Color.Red;
                button2.Visible = true;
                button4.Visible = false;
                button2.Enabled = false;
            }
            else
                // GAME IS running
            {
                status.Text = @"GAME is Running!";
                uGAME.ForeColor = Color.Green;
                button2.Visible = false;
                button4.Visible = true;
            }
        }

        // Verify bot and GAME are running before starting 
        public void BotReady(object sender, EventArgs e)
        {

            CheckStatus(sender, e);

            if (uGAME.ForeColor == Color.Green)
            {

                    status.Text = @"Bot Started!";
                    Botting(sender, e);

            }
            else { status.Text = @"GAME is not running"; }
        }

        //THE BOT ACTIONS
        public void Botting(object sender, EventArgs e)
        {
            // Check to make sure everything is still running
            CheckStatus(sender, e);
            TargetAQ(sender, e);

            if (uGAME.ForeColor == Color.Green)
            {

                    // all is running, then you good.
                    Script(sender, e);

            }

            //GAME died, kill scripts
            else { 
                KillBot(sender, e);
                status.Text = @"GAME Crashed:(";
            }

        }

        //Things it does in-game
        public void Script(object sender, EventArgs e)
        {
            status.Text = @"Bot in progress..";

            // Use skills in game rotation
            System.Threading.Thread.Sleep(3000);
            SendKeys.Send("1");
            System.Threading.Thread.Sleep(3000);
            SendKeys.Send("2");
            System.Threading.Thread.Sleep(3000);
            SendKeys.Send("3");
          
            // Go back and check the game has not crashed before re-running the script
            Botting(sender, e);
        }

        // STOP THE BOT AND TIME COUNTER
        public void KillBot(object sender, EventArgs e)
        {
            // kill the clock and bot
            status.Text = @"Stopping bot...";
            timer.Stop();
            sw.Stop();
            label2.Text = sw.Elapsed.Seconds.ToString() + " seconds";
            status.Text = @"Bot Stopped";
        }
    }
}
DeadLink
  • 93
  • 12
  • PS. using Notepad as a filler in place of the game. – DeadLink Aug 13 '20 at 00:43
  • your sleep() is running on the main thread (handle UI), thus, block your UI -> you can use backgroundworker/ thread/ task ... et cetera... to handle it. – MarioWu Aug 13 '20 at 00:58
  • _System.Threading.Thread.Sleep, does not let other functions of the app preform_ yes, you're telling the UI thread to sleep. – stuartd Aug 13 '20 at 00:58
  • That makes sense, its not delaying the function, its delaying the whole app. Is there a different script to just delay the function and not the whole program? like a class Im missing – DeadLink Aug 13 '20 at 01:01
  • @JakeJigsaw you can follow this comment instruction. https://stackoverflow.com/a/11500805/10505137 – MarioWu Aug 13 '20 at 01:05
  • @MarioWu I will give that a try, thank you :] – DeadLink Aug 13 '20 at 02:28

0 Answers0