首页 > 解决方案 > 循环脚本不更新其他表单

问题描述

长话短说,我正在制作的应用程序将启动游戏。然后“开始”按钮将检查以确保游戏 EXE 正在运行。如果它正在运行,它将运行一个脚本来按下按钮 1、2 和 3。之后它将循环该脚本,但首先检查是否游戏没有崩溃(如果它崩溃了它不会运行循环)

我的问题:

在使用 System.Threading.Thread.Sleep 的循环运行时,不会让应用程序的其他功能预执行(在这种情况下显示和隐藏按钮,以及更改标签颜色。)这很重要的主要原因是按钮它不会是应该结束循环脚本的停止按钮。

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";
        }
    }
}

标签: c#winforms

解决方案


推荐阅读