如果用户想要而不是退出应用程序,我只是试图通过单击按钮停止进程来中止线程。
这是原始代码:
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
}
我试过的:
我通过将线程字段移出方法来尝试从按钮单击事件中的 Abort() 线程,这样您就可以从按钮单击事件中获取访问权限,但它会引发许多错误:
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()));
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( )
{
thread1.Start();
thread1.Join(); //Waiting for thread to finish
}
这就是我所做的,但我得到了错误:
在 e 和 genericlist 下:字段初始化不能引用非静态字段、方法或属性。