我正在做winforms应用程序,它可以在堡垒之夜运行时自动运行Wooting运动,但是我需要线程这个函数。我不能用System.Threading.Tasks来连接它。我试着使用Thread.Start和Thread.Abort,但是Thread.Abort在.NET Core5.0中不起作用。那我该怎么做呢?Button4是开始按钮,Button5是停止
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;
}
}
}
}如果你不明白,对不起,我不是美国人
发布于 2022-02-03 13:50:42
的
https://stackoverflow.com/questions/70972131
复制相似问题