C# Open Form 和 运行 同时函数

C# Open Form and run fuction at the same time

我正在尝试为游戏编写记忆作弊代码,但我想创建一个菜单,所以我决定制作它。现在我希望我的程序在打开表格的同时进行作弊。因为现在作弊正在做,否则作弊不起作用并且表格正在打开。 我是 C# 的新手,很抱歉,如果我是菜鸟……:P

谢谢, 伊兹米歇尔

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

namespace ac_client_cheat
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Main();
        }

        public static int Pbase = 0x00509B74;
        public static int Health = 0xf8;
        public static int mgammo = 0x150;
        public static int fly = 0x44;
        public void Main()
        {
            VAMemory vam = new VAMemory("ac_client");
            int localplayer = vam.ReadInt32((IntPtr)Pbase);
            {
                while (true)
                {
                   int address = localplayer + fly;
                    float aimlock = vam.ReadFloat((IntPtr)address);
                    vam.WriteFloat((IntPtr)address, -5);

                    address = localplayer + Health;
                    vam.WriteInt32((IntPtr)address, 1337);

                    address = localplayer + mgammo;
                    vam.WriteInt32((IntPtr)address, +1337);
                }
            }
        }
    }
}

Windows 表单应用程序以 single-threaded 开始,因此您不能同时做两件事。

处理这个问题的典型方法是启动一个工作线程,例如使用 Task.Run.

但是,由于您的应用程序非常简单,我们可以使用 Application.DoEvents 相反。

当您调用 DoEvents 时,您的线程将检查表单的消息循环并处理任何未决事件,例如菜单点击或其他事件。如果没有未决事件,它将让您的主循环恢复。这个方案可以让你摆脱 运行 在同一个线程上的表单和循环。

while (true)
{
    int address = localplayer + fly;
    float aimlock = vam.ReadFloat((IntPtr)address);
    vam.WriteFloat((IntPtr)address, -5);

    address = localplayer + Health;
    vam.WriteInt32((IntPtr)address, 1337);

    address = localplayer + mgammo;
    vam.WriteInt32((IntPtr)address, +1337);

    Application.DoEvents();  //Yield control to the message loop
}

此外,请确保您使用 Application.Run 开始消息循环。有关详细信息,请参阅 this answer。这是一种方法:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

你的代码的问题是构造函数永远不会退出,所以表单永远不会显示。

从您的构造函数中删除对 Main 的调用,并将其放入 Timer 事件处理程序中。由于计时器会为您处理重复,因此您也可以删除 while 循环。

您可以同时显示表格和 运行 您的作弊循环,如下所示:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public static int Pbase = 0x00509B74;
    public static int Health = 0xf8;
    public static int mgammo = 0x150;
    public static int fly = 0x44;

    /// <summary>The main entry point for the application.</summary>
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var f = new Form1();
        f.Show();

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

        while (f.Visible) //loop while form is not closed
        {
            int address = localplayer + fly;
            float aimlock = vam.ReadFloat((IntPtr)address);
            vam.WriteFloat((IntPtr)address, -5);

            address = localplayer + Health;
            vam.WriteInt32((IntPtr)address, 1337);

            address = localplayer + mgammo;
            vam.WriteInt32((IntPtr)address, +1337);

            Application.DoEvents();  //Yield control to the message loop
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //...
    }
}