首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何开始线程化函数并通过任务按钮停止线程处理

如何开始线程化函数并通过任务按钮停止线程处理
EN

Stack Overflow用户
提问于 2022-02-03 13:27:54
回答 1查看 46关注 0票数 0

我正在做winforms应用程序,它可以在堡垒之夜运行时自动运行Wooting运动,但是我需要线程这个函数。我不能用System.Threading.Tasks来连接它。我试着使用Thread.Start和Thread.Abort,但是Thread.Abort在.NET Core5.0中不起作用。那我该怎么做呢?Button4是开始按钮,Button5是停止

代码语言:javascript
复制
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading.Tasks;

namespace AutoDoubleMovement
{
    public partial class Form1 : Form
    {
        static OpenFileDialog ofd = new OpenFileDialog();

        static Settings Settings = new Settings();
        static Info Info = new Info();

        static string path = $@"C:\Users\{Environment.UserName}\AppData\Local\AutoWDM";
        static string file = $@"{path}\WDM_path.txt";
        static string file_wdm = File.ReadAllText(file);

        // Threading this function
        public static void WDM()
        {
            while (true)
            {
                string wdm_path = File.ReadAllText(file);
                Process[] fortnite = Process.GetProcessesByName("FortniteClient-Win64-Shipping");
                Process[] wdm = Process.GetProcessesByName("wooting-double-movement");
                ProcessStartInfo wdmproc = new ProcessStartInfo() { FileName = wdm_path };

                foreach (Process _wdm in wdm)
                {
                    if (fortnite.Length != 0)
                    {
                        if (wdm.Length == 0)
                        {
                            Process.Start(wdmproc);
                        }
                    }
                    else
                    {
                        if (wdm.Length != 0)
                        {
                            _wdm.Kill();
                        }
                    }
                    Thread.Sleep(1000);
                }
            }
        }

        public Form1()
        {
            InitializeComponent();
            Settings.Owner = this;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() != DialogResult.Cancel)
            {
                textBox1.Text = ofd.FileName;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                    if (Directory.Exists(path))
                    {
                        File.WriteAllText(file, ofd.FileName);
                    }
                }
                else
                {
                    File.WriteAllText(file, ofd.FileName);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Settings.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Info.ShowDialog();
        }

        // This is the button to start threading WDM
        private void button4_Click(object sender, EventArgs e)
        {
            if (!textBox1.Text.Contains("wooting-double-movement.exe"))
            {
                MessageBox.Show("Your path doesn't contain \"wooting-double-movement.exe\" file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                button1.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = true;
            }
        }

        // This is the button to stop threading WDM
        private void button5_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(file))
            {
                textBox1.Text = file_wdm; 
            }
        }
    }
}

如果你不明白,对不起,我不是美国人

EN

回答 1

Stack Overflow用户

发布于 2022-02-03 13:50:42

  1. 不使用静态OpenFileDialog ofd =新的OpenFileDialog ();只使用使用.Start()和.Abort()

  • 尝试不使用任务,而使用普通线程。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70972131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档