线程用webrequest请求,用webresponse接收数据,然后写入文件.
点开始按钮,可以下载完成.我加个停止按钮button2,想点它以后终止线程,始终报错,大家给个正确的写法吧
还有一点不明白,下载过程中线程状态一直是waitsleephoin,不是running.怎么写它才能正常?
代码如下:
namespace WindowsApplication8
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button2;
private static string StrUrl="http://www.xfocus.net/tools/200605/IceSword1.18en.rar"; //根据实际情况设置
private static System.Net.HttpWebRequest request =(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
private static System.IO.FileStream fs;
private static System.Net.HttpWebResponse response=(System.Net.HttpWebResponse)request.GetResponse();
private static System.IO.Stream ns= response.GetResponseStream();
private System.Windows.Forms.Label label3;
private static Thread downth;
private static int nReadSize=0;
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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button2 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(144, 272);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(88, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(264, 88);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 144);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(240, 88);
this.label2.TabIndex = 2;
this.label2.Text = "label2";
//
// button2
//
this.button2.Location = new System.Drawing.Point(328, 280);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "停止";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(80, 240);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(168, 23);
this.label3.TabIndex = 4;
this.label3.Text = "label3";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 349);
this.Controls.Add(this.label3);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(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)
{
downth = new Thread(new ThreadStart(down));
downth.Start();
System.Timers.Timer t = new System.Timers.Timer(1 * 500);
t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
t.Enabled = true;
}
private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e)
{
FileInfo objFI = new System.IO.FileInfo("c:\\1.rar");
long lens = objFI.Length;
label2.Text=lens.ToString();
label3.Text=downth.ThreadState.ToString();
}
private void down()
{
string StrFileName="c:\\1.rar"; //根据实际情况设置
//打开上次下载的文件或新建文件
long lStartPos =0;
if (System.IO.File.Exists(StrFileName))
{
fs= System.IO.File.OpenWrite(StrFileName);
lStartPos=fs.Length;
fs.Seek(lStartPos,System.IO.SeekOrigin.Current); //移动文件流中的当前指针
}
else
{
fs = new System.IO.FileStream(StrFileName,System.IO.FileMode.Create);
lStartPos =0;
}
//打开网络连接
try
{
if (lStartPos>0)
request.AddRange((int)lStartPos); //设置Range值
//向服务器请求,获得服务器回应数据流
byte[] nbytes = new byte[512];
nReadSize=ns.Read(nbytes,0,512);
while( nReadSize >0)
{
fs.Write(nbytes,0,nReadSize);
nReadSize=ns.Read(nbytes,0,512);
}
fs.Close();
ns.Close();
MessageBox.Show("下载完成");
}
catch(Exception ex)
{
fs.Close();
MessageBox.Show("下载过程中出现错误:"+ex.ToString());
}
}
private void button2_Click(object sender, System.EventArgs e)
{
fs.Close();
ns.Close();
//response.Close();
downth.Abort();
//到这已经晕了...试了N多方法 就是不能成功停止线程...一直报错
}
}
}