国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

c#使用線程下載文件的控制技巧和缺陷

2024-07-21 02:26:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

//c#使用線程下載文件的控制技巧和缺陷

//系統(tǒng)引用//定義線程公共變量//開(kāi)始線程下載文件//中止線程下載

//系統(tǒng)引用

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.net;
using system.io;
using system.text;
using system.threading;
using system.data;

//定義線程公共變量
  public system.threading.thread thread001;
  public system.threading.thread thread002;

   //開(kāi)始線程下載文件

private void button7_click(object sender, system.eventargs e)
  {
   //開(kāi)始線程下載文件
   downloadclass a=new downloadclass(); 
   thread001= new thread(new threadstart(a.downloadfile));
   a.strurl=textbox1.text;
   a.strfilename=textbox2.text;   
   thread001.start();
   downloadclass b=new downloadclass(); 
   thread002= new thread(new threadstart(b.downloadfile));
   b.strurl=textbox3.text;
   b.strfilename=textbox4.text;   
   thread002.start();
  }

   //中止線程下載

private void button5_click(object sender, system.eventargs e)
  {
   //中止線程下載
   thread001.abort();  
   thread002.abort();
   messagebox.show("線程已經(jīng)中止!","警告!");
   
  }

 

//定義下載文件類.線程傳參數(shù)用

  public class downloadclass
  {  
   //打開(kāi)上次下載的文件或新建文件
   public string strurl;//文件下載網(wǎng)址
   public string strfilename;//下載文件保存地址
   public string strerror;//返回結(jié)果
   public long lstartpos =0; //返回上次下載字節(jié)
   public long lcurrentpos=0;//返回當(dāng)前下載字節(jié)
   public long ldownloadfile;//返回當(dāng)前下載文件長(zhǎng)度

   public void downloadfile()
    {    
    system.io.filestream fs;
    if (system.io.file.exists(strfilename))
    {
     fs= system.io.file.openwrite(strfilename);
     lstartpos=fs.length;
     fs.seek(lstartpos,system.io.seekorigin.current);
     //移動(dòng)文件流中的當(dāng)前指針
    }
    else
    {
     fs = new system.io.filestream(strfilename,system.io.filemode.create);
     lstartpos =0;
    }

    //打開(kāi)網(wǎng)絡(luò)連接
    try
    {
     system.net.httpwebrequest request =(system.net.httpwebrequest)system.net.httpwebrequest.create(strurl);
     long length=request.getresponse().contentlength;
     ldownloadfile=length;
     if (lstartpos>0)
      request.addrange((int)lstartpos); //設(shè)置range值
    
     //向服務(wù)器請(qǐng)求,獲得服務(wù)器回應(yīng)數(shù)據(jù)流
     system.io.stream ns= request.getresponse().getresponsestream();
    
     byte[] nbytes = new byte[512];
     int nreadsize=0;    
     nreadsize=ns.read(nbytes,0,512);
     while( nreadsize >0)
     {
      fs.write(nbytes,0,nreadsize);
      nreadsize=ns.read(nbytes,0,512);
      lcurrentpos=fs.length; 
   
     }
    
     fs.close();
     ns.close();
     strerror="下載完成";
    
    }
    catch(exception ex)
    {
     fs.close();
     strerror="下載過(guò)程中出現(xiàn)錯(cuò)誤:"+ ex.tostring();
    
    }
  
   }
  } 
  //定義下載類結(jié)束 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 景泰县| 通许县| 宁远县| 安国市| 平安县| 德保县| 平定县| 信阳市| 开平市| 惠东县| 嘉义县| 安西县| 瑞金市| 遂川县| 海安县| 当涂县| 正镶白旗| 龙游县| 咸阳市| 宜春市| 恩施市| 兰坪| 安陆市| 千阳县| 罗平县| 九寨沟县| 呼玛县| 鄂托克旗| 车致| 河北省| 运城市| 伊宁市| 新郑市| 台南市| 偃师市| 桂平市| 麻江县| 留坝县| 紫云| 察隅县| 普格县|