如果用户需要,我只是尝试通过单击按钮停止进程而不是退出应用程序来中止线程。
这是原始代码:
private void Abort_Click(object sender, EventArgs e)
{
// thread1.Abort();
}
/// Runs method 'Copy' in a new thread with passed arguments - on this way we separate it from UI thread otherwise it would freeze
private void backgroundCopy_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e )
{
List<object> genericlist = e.Argument as List<object>;
Thread thread1 = new Thread(() => Copy(genericlist[0].ToString(), genericlist[1].ToString()));
thread1.Start();
thread1.Join(); //Waiting for thread to finish
}
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
我通过将线程字段移出方法来尝试从按钮单击事件中 Abort() 线程,这样您就可以从按钮单击事件中获得访问权限,但它会引发许多错误:
object sender;
System.ComponentModel.DoWorkEventArgs e;
List<object> genericlist = e.Argument as …
Run Code Online (Sandbox Code Playgroud)