求“点一下button按钮就可以停止拷贝的多线程序” 现在有一个线程进行一个大文件的拷贝 我想在界面上放一个“取消”按钮,按下“取消”就停止拷贝我是个新手 谢谢给个代码让我研究(分不够可以加) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static Thread copy;...按钮click事件里:class1.copy.Abort(); 就在click 事件中加Abort();马能不能具体点 我不太熟悉 谢谢 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(); } }} public class Form1 : System.Windows.Forms.Form{ private Thread x; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; 谢谢 晚上去试 另外再提个问题 我在同一个namespace中写了2个类 并且在一个类中实列化了另一个类 但是这个对象好象不能用(即: 对象的方法\属性) 这是为什么 停止好办。加一个全局的私有变量 bool _isStopFired = false在停止按钮里加上 _isStopFired = true在拷贝的循环里加上 if(!_isStopFired){ //拷贝的内容}else{ //关闭流。 return} Thread x这个定义在button1的作用域里,button2不能访问把它定义成公共的 引用XML schema,怎么不报错啊 导出到excel多个sheet 大家来讨论:在企业级应用中的数据并发控制方案 window.open打开无关闭按钮的窗口 Thinksea 冗余文件清理工具 v2.0.0.0 (开源) 关于泛型的问题 一个DataSet已经通过ReadXmlSchema 读入了结构,怎样把数据(XML串)读进来? 请问VB.NET的with语句翻译到C#中是什么? 请问这个例子怎样运行? 成立c#的学习小组 能不能用索引的方法获取。 Response.Redirect()如何弹出一个新窗口?
...
按钮click事件里:class1.copy.Abort();
能不能具体点 我不太熟悉 谢谢
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();
}
}}
{
private Thread x;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
另外再提个问题 我在同一个namespace中写了2个类 并且在一个类中实列化了另一个类 但是这个对象好象不能用(即: 对象的方法\属性) 这是为什么
在停止按钮里加上 _isStopFired = true
在拷贝的循环里加上
if(!_isStopFired)
{
//拷贝的内容
}
else
{
//关闭流。
return
}