现在有一个线程进行一个大文件的拷贝  我想在界面上放一个“取消”按钮,按下“取消”就停止拷贝
我是个新手  谢谢给个代码让我研究(分不够可以加)

解决方案 »

  1.   

    public static Thread copy;
    ...
    按钮click事件里:class1.copy.Abort();
      

  2.   

    就在click 事件中加Abort();马
    能不能具体点  我不太熟悉  谢谢
      

  3.   

    2个按钮(开始,停止) 现在“开始”可以了  但是“停止”不知道怎么写 求高人就我
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    using System.Threading;namespace button_writethread
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(176, 80);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(56, 24);
    this.button1.TabIndex = 0;
    this.button1.Text = "start";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(176, 120);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(56, 24);
    this.button2.TabIndex = 1;
    this.button2.Text = "stop";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button2,
      this.button1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    { readwrite a=new readwrite();
    Thread x=new Thread(new ThreadStart(a.rw_action));
    x.Start(); } private void button2_Click(object sender, System.EventArgs e) //停止
    {

    }
    }
    public  class  readwrite
    {
    FileStream fs1;
    BinaryReader binReader;
    FileStream fs2;
    BinaryWriter binWriter;
    byte [] buff=new Byte[1024];
    int x;
    int i;
    public  readwrite()
    {
    fs1 = new FileStream("F:\\MOVE\\H\\a.avi", FileMode.Open, FileAccess.Read);
    fs2 = new FileStream("F:\\MOVE\\H\\b.avi", FileMode.CreateNew);
    binReader=new BinaryReader(fs1);
    binWriter=new BinaryWriter(fs2);
    }
    public  void  rw_action()
    { while (fs1.Length >i)
    {
    x=binReader.Read(buff,0,1024);
    binWriter.Write(buff,0,1024);
    i=i+1024;
    }
    binReader.Close();
    binWriter.Flush();
    binWriter.Close();
    }
    }}
      

  4.   

    public class Form1 : System.Windows.Forms.Form
    {
    private Thread x;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
      

  5.   

    谢谢  晚上去试  
    另外再提个问题  我在同一个namespace中写了2个类  并且在一个类中实列化了另一个类  但是这个对象好象不能用(即: 对象的方法\属性)  这是为什么
      

  6.   

    停止好办。加一个全局的私有变量 bool _isStopFired = false
    在停止按钮里加上 _isStopFired = true
    在拷贝的循环里加上
     if(!_isStopFired)
    {
       //拷贝的内容
    }
    else
    {
       //关闭流。
      return
    }
      

  7.   

    Thread x这个定义在button1的作用域里,button2不能访问把它定义成公共的