C# 如何在c中处理窗体上的多个事件#

C# 如何在c中处理窗体上的多个事件#,c#,C#,在我当前的项目中,我有一个表单,其中包含两个名为“复制”和“取消”的命令按钮 如果单击“复制”按钮,它将3000个文件从源目录复制到目标目录,同时如果单击“取消”按钮,它将取消复制并退出表单。有什么办法吗 我应用了解决方案,但出现了错误。我有以下代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using

在我当前的项目中,我有一个表单,其中包含两个名为“复制”和“取消”的命令按钮 如果单击“复制”按钮,它将3000个文件从源目录复制到目标目录,同时如果单击“取消”按钮,它将取消复制并退出表单。有什么办法吗

我应用了解决方案,但出现了错误。我有以下代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        private volatile bool _continue = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _continue = true;
            System.Threading.ThreadStart ts = new ThreadStart(print_number);
            System.Threading.Thread t = new Thread(ts);
            t.Start();
        }
        private void print_number()
        {
            for (int i = 1; i <= 10000; i++)
            {
                textBox1.Text = Convert.ToString(i);
                if (_continue == false)
                {
                    return;
                }

                //Thread.Sleep(2000);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _continue = false;
            Close();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统线程;
命名空间Windows窗体应用程序8
{
公共部分类Form1:Form
{
private volatile bool_continue=false;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
_继续=真;
System.Threading.ThreadStart ts=新的ThreadStart(打印编号);
System.Threading.Thread t=新螺纹(ts);
t、 Start();
}
私人作废打印号()
{
对于(int i=1;i示例:

private volatile bool _continue = false;

private void CopyClicked(Object sender, EventArgs e)
{
   _continue = true;
   System.Threading.ThreadStart ts = new ThreadStart(CopyFiles);
   System.Threading.Thread t = new Thread(ts);
   t.Start();
}

private void CopyFiles(){
   List<String> list = GetFileNames();
   foreach( String f in list )
   {
      if ( _continue == false )
      {
        return;
      }
      CopyFile(s);  //examples...
   }

}
private void CancelClicked(Object sender, EventArgs e)
{
    _continue = false;
    Close();
}
private volatile bool\u continue=false;
私有void CopyClicked(对象发送方,事件参数e)
{
_继续=真;
System.Threading.ThreadStart ts=新的ThreadStart(复制文件);
System.Threading.Thread t=新螺纹(ts);
t、 Start();
}
私有void CopyFiles(){
List=GetFileNames();
foreach(列表中的字符串f)
{
如果(_continue==false)
{
返回;
}
复制文件;//示例。。。
}
}
私有void取消单击(对象发送方,事件参数e)
{
_continue=false;
Close();
}

C#FAQ回答了这个问题:(有很多方法可以做到这一点;该链接演示了一种有效的方法。)如何执行将文件从源复制到目标的任务?只有在工作线程上执行复制操作时,才能取消复制操作,否则UI线程将无法响应单击“取消”按钮,因为UI线程将忙于执行复制操作。i我在System.IO中使用FILE.copy()方法;是的,有一种方法可以做到这一点。做一些研究,尝试一些代码,遇到问题时再回来。看看线程。
\u continue
应该用关键字标记