WPF多线程下载文件,有进度条
//打开对话框选择文件
private void OpenDialogBox_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "*|*";
openFileDialog.RestoreDirectory = true;
openFileDialog.ShowDialog();
//包含路径的文件名
var fileNames = openFileDialog.FileName;
//文件名
var name = openFileDialog.SafeFileName;
DownLoadBox.Text = fileNames;
DownLoadBox.Tag = name;
}
//点击下载下载文件
private void DownLoadButton_Click(object sender, RoutedEventArgs e)
{
string filePath = DownLoadBox.Text;
string name = DownLoadBox.Tag.ToString();
if (string.IsNullOrEmpty(name)) return;
if (filePath == null) return;
var file = new FileInfo(filePath);
//取到文件的总想长度
long length = file.Length;
#region 添加进度条
var stackpael = new StackPanel()
{
Margin = new Thickness(0, 3, 0, 5)
};
var lable = new System.Windows.Controls.Label();
var progressbar = new ProgressBar()
{
Margin = new Thickness(10, 10, 0, 13),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Width = 300,
Height = 30,
Maximum = length
};
stackpael.Children.Add(lable);
stackpael.Children.Add(progressbar);
DownLoadStackPanel.Children.Add(stackpael);
//创建实体,以方便多线程传值
Controls control = new Controls();
control.Lable = lable;
control.Name = name;
control.Progressbar = progressbar;
control.Path = filePath;
#endregion
//使用线程池下载文件
ThreadPool.QueueUserWorkItem(new WaitCallback(DownLoad), control);
}
#region 使用线程池下载文件void DownLoad(object control)
/// <summary>
/// 使用线程池下载文件
/// </summary>
/// <param name="control"> 参数</param>
private void DownLoad(object control)
{
var controls = control as Controls;
string name = controls.Name;
string filePath = controls.Path;
System.Windows.Controls.Label lable = controls.Lable;
ProgressBar progressBar = controls.Progressbar;
//存放下载文件的路径
string newPath = Path.GetFullPath("../../File");
if (name == null || filePath == null || lable == null || progressBar == null) return;
//下载
DownLoadFile(name, filePath, newPath, lable, progressBar);
}
#endregion
#region 下载void DownLoadFile(string name, string filePath, string newPath, System.Windows.Controls.Label lable, ProgressBar progressBar)
/// <summary>
/// 下载
/// </summary>
/// <param name="name">文件名</param>
/// <param name="filePath">文件路径</param>
/// <param name="newPath">存放文件的路径</param>
/// <param name="lable">存放文件名的控件</param>
/// <param name="progressBar">进度条</param>
private void DownLoadFile(string name, string filePath, string newPath, System.Windows.Controls.Label lable, ProgressBar progressBar)
{
newPath = newPath + "\\" + Guid.NewGuid() + name;
//FileMode.Create创建文件 FileMode.Open 打开文件
using (Stream so = new FileStream(newPath, FileMode.Create))
{
using (Stream st = new FileStream(filePath, FileMode.Open))
{
byte[] by = new byte[1024 * 1024 * 100];
int osize = st.Read(by, 0, (int)by.Length);
int lengths = osize;
//被主线程调用
Action a = () =>
{
lable.Content = name;
};
this.Dispatcher.BeginInvoke(a);
while (osize > 0)
{
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
lengths += osize;
Action action = () =>
{
//进度条
progressBar.Value = lengths;
};
progressBar.Dispatcher.BeginInvoke(action);
}
}
}
}
#endregion