wp7上实现多线程下载,和在其他地方有点区别,wp7现阶段一些c#中的code不能直接使用,贴出自己写的demo,(并没写断点续传 如果想做,就必须给 request的 Headers 添加属性添加一个头 “Content=bytes” 下载是 使用stream.Seek() 设置“流头” 从当前位置开始下载)
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
namespace Mp3StoreandPlayback
{
public class DownloadThread
{
public double PerSecondStreamLegth{get;set;}
public double TotalLeght { get; set; }
public double currentLeght{get;set;}
public string Loadspead { get; set; }
public double myvalue { get; set; }
public Dictionary<int, DownloadThread> mydownThread;
public DownloadThread()
{
mydownThread = new Dictionary<int, DownloadThread>();
}
public Dictionary<int,DownloadThread> AddTODic(int key, DownloadThread thread)
{
if (!mydownThread.ContainsKey(key))
{
mydownThread.Add(key, thread);
}
return mydownThread;
}
public string jisuan()
{
int totalSecond = 0;
if (currentLeght != 0)
{
PerSecondStreamLegth = currentLeght / TotalLeght;
if (PerSecondStreamLegth >= 1)
{
this.myvalue = 100;
}
this.myvalue = PerSecondStreamLegth * 100;
double temp = Math.Ceiling(PerSecondStreamLegth * 100);
Loadspead = string.Format("{0:p}", temp);
Loadspead = temp + "%";
totalSecond += 500;
if (totalSecond / 1000 > TotalLeght / 1024)
{
Loadspead = "超时";
}
//报告下载进度
}
return Loadspead;
}
}
}
复制代码
使用简单的 页面实现下载,每条下载项可以是从网络中下载的。
?
public DownLoadPage()
{
InitializeComponent();
task = new DownloadMusicTask();
this.textBox1.Text = "http://files.sparklingclient.com/079_2009.08.20_ElementBinding.mp3";
this.textBox2.Text = "http://f3.xiami.net/57089/353565/01%201769196206_851967.mp3";
this.textBox3.Text = "http://api.ning.com/files/ZTlTyJnBfjXPa45lorAkBLu5dIaGg4KFya*dsNBYoMZT3dR0Vgq*aV2OOeRxj9rExENLOgkSvs8ZCbSJ8bEsFG5lGf4Moz*F/love.mp3";
this.textBox4.Text = "http://222.174.95.18:8068/musicfile/music/40/39295.wma";
this.textBox5.Text = "http://wap3.shenglong.com.cn/ring/20080925170158617.wma";
this.textBox6.Text = "http://wap3.shenglong.com.cn/ring/20080925170158617.wma";
}
//下载所有
private void DownLoad_Click(object sender, RoutedEventArgs e)
{
}
//取消下载
private void CancelDownLoad_Click(object sender, RoutedEventArgs e)
{
}
//暂停下载
private void StopDowload_Click(object sender, RoutedEventArgs e)
{
}
//单个下载
private void button1_Click(object sender, RoutedEventArgs e)
{
task.DownloadUrl = this.textBox1.Text.Trim();
download();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
task.DownloadUrl = this.textBox2.Text.Trim();
download();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
task.DownloadUrl = this.textBox3.Text.Trim();
download();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
task.DownloadUrl = this.textBox4.Text.Trim();
download();
}
private void button5_Click(object sender, RoutedEventArgs e)
{
task.DownloadUrl = this.textBox5.Text.Trim();
download();
}
private void button6_Click(object sender, RoutedEventArgs e)
{
task.DownloadUrl = this.textBox6.Text.Trim();
download();
}
//多线程
public void download()
{
fileName = System.IO.Path.GetFileName(task.DownloadUrl);
task.MusicSavePath = FileManager.GetSavaPath(fileName,MusicFormats.MP3.ToString());
if (string.IsNullOrEmpty(task.MusicSavePath))
{
task = null;
MessageBox.Show("无法保存文件");
}
task.DownloadTaskID = Guid.NewGuid();
downloadManager.RunWorker(task);
//Deployment.Current.Dispatcher.BeginInvoke(delegate() { down.RunWorker(task); });
}