我正在尝试进行设置,因此当我单击按钮时,它会关闭/中止当前正在运行的线程。我也尝试过使用Thread.IsAlive.ToString,但是即使我看到它总是返回false线程正在运行(仍在richtextbox上显示文本)。
Thread scheduler => new Thread(Work);
public void Work()
{
SetText();
}
private void Button10_Click(object sender, EventArgs e)
{
//button that should stop thread
scheduler.Abort();
}
private void WriteTextSafe(string text)
{
if (richTextBox1.InvokeRequired)
{
var d = new SafeCallDelegate(WriteTextSafe);
Invoke(d, new object[] { text });
}
else
{
richTextBox1.AppendText(text);
}
}
private void SetText()
{
WriteTextSafe("This text was set safely.\n");
System.Threading.Thread.Sleep(3000);
WriteTextSafe("This text was set safely.\n");
}