wp7之《够酷悦音播放器》 使用线程池实现多线程下载
2012-03-13 17:59
316 查看
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); });
}
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); });
}
相关文章推荐
- wp7之《够酷悦音播放器》 使用线程池实现多线程下载 (二)断点续传下载
- 线程池的使用和实现
- 使用ExecutorService实现线程池
- ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
- ActiveMQ使用线程池实现消息的生产与消费
- 使用Java并发包线程池和XML实现定时任务动态配置和管理
- 线程池的原理以及实现线程池的类ExecutorService中方法的使用
- ThreadPoolExecutor使用和思考-线程池大小设置与BlockingQueue的三种实现
- 在python中使用concurrent.futures实现进程池和线程池
- ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
- 福利贴:使用线程池实现的Http上传下载实现
- 使用requests库实现多线程下载
- Java并发核心基础——线程池使用及底层实现机制详解
- ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
- ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
- ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
- 使用C++11 实现的线程池
- ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别(总结)
- [20170305]使用JAVA实现Http多线程下载
- 使用线程池来实现AsyncTask的排队机制与拒绝策略