'How to start threading the function and stop threading by the buttons with Tasks
I'm doing winforms app that can automatically run the Wooting-Double-Movement when Fortnite is running, but I need to thread the function. I can't thread it by System.Threading.Tasks. I tried to use Thread.Start and Thread.Abort but Thread.Abort doesn't work in .NET Core 5.0. So, how can I do this? Button4 is start button and Button5 is stop
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;
}
}
}
}
And if you didn't understand something, sorry for that, I'm not American
Solution 1:[1]
- DO NOT USE static OpenFileDialog ofd = new OpenFileDialog(); Make dialogs only with using () { }
- Try not Task but normal Thread, with .Start() and .Abort()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yotic |
