I am developing Download Manager and I am HttpWebRequest but my progress bar's value is not changing. Can you tell me What is wrong?
I have tried pb.value = (example)
but its not working. I have wrote this line in TASK.Start thred
What can I do??
Thanks in Advance..
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows;
using System.Net;
using System.Threading;
using System.Reflection;
namespace DownloadManager
{
public partial class Download : Window,INotifyPropertyChanged
{
public Download()
{
InitializeComponent();
}
private int m_Progress;
public int Progress
{
get { return m_Progress; }
set
{
if (value == m_Progress) return;
m_Progress = value;
OnPropertyChanged("Progressb");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs("Progressb"));
Application.Current.Dispatcher.Invoke(() => {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Progressb"));
}, System.Windows.Threading.DispatcherPriority.Background);
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Progressb"));
}
}
Uri uri;
static WebClient client = new WebClient();
static int row = 0;
public void download(string link, string path)
{
uri = new Uri(link);
String FileName = System.IO.Path.GetFileName(uri.AbsolutePath);
client.DownloadFileAsync(uri, path +@"\" + FileName);
client.DownloadProgressChanged += downloadprogress;
FileName1.Content = FileName;
}
double Percentage;
public void downloadprogress(object sender, DownloadProgressChangedEventArgs e)
{
pb.Minimum = 0;
double receive = double.Parse(e.BytesReceived.ToString());
double Filesize = double.Parse(e.TotalBytesToReceive.ToString());
Percentage = receive / Filesize * 100;
pb.Value = int.Parse(Math.Truncate(Percentage).ToString());
Download download = new Download();
int p = (int) Percentage;
MainWindow m = Application.Current.MainWindow as MainWindow;
m.change(download,p.ToString(),row);
Status.Content = "Downloading.";
Status.Content = "Downloading..";
Status.Content = "Downloading...";
if (pb.Value == 100)
{
row++;
Status.Content = "Completed";
Close();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
private volatile bool _allowedToRun;
private string _source;
private string _destination;
private int _chunkSize;
Download d1;
private IProgress<double> _progress;
private Lazy<int> _contentLength;
public int BytesWritten { get; private set; }
public int ContentLength { get { return _contentLength.Value; } }
public bool Done { get { return ContentLength == BytesWritten; } }
public Download(string source, string destination, int chunkSize, IProgress<double> progress = null)
{
_allowedToRun = true;
_source = source;
_destination = destination;
_chunkSize = chunkSize;
_contentLength = new Lazy<int>(() => Convert.ToInt32(GetContentLength()));
_progress = progress;
BytesWritten = 0;
}
private long GetContentLength()
{
var request = (HttpWebRequest)WebRequest.Create(_source);
request.Method = "HEAD";
using (var response = request.GetResponse())
return response.ContentLength;
}
static public int p9;
private async Task Start(int range)
{
if (!_allowedToRun)
throw new InvalidOperationException();
var request = (HttpWebRequest)WebRequest.Create(_source);
request.Method = "GET";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
request.AddRange(range);
using (var response = await request.GetResponseAsync())
{
using (var responseStream = response.GetResponseStream())
{
using (var fs = new FileStream(_destination, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
while (_allowedToRun)
{
var buffer = new byte[_chunkSize];
var bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead == 0) break;
await fs.WriteAsync(buffer, 0, bytesRead);
BytesWritten += bytesRead;
double receive = double.Parse(BytesWritten.ToString());
double Filesize = 5761048;
double Percentage = receive / Filesize * 100;
int per = int.Parse(Math.Truncate(Percentage).ToString());
Progress = per;
await Task.Delay(20);
}
await fs.FlushAsync();
}
}
}
}
public Task Start()
{
_allowedToRun = true;
return Start(BytesWritten);
}
public void Pause1()
{
_allowedToRun = false;
}
void MakeButton2()
{
Button b2 = new Button();
}
object sender;
public void Onb2Click2(object sender, RoutedEventArgs e)
{
}
void OnLoad(object sender, RoutedEventArgs e)
{
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
int m=0;
}
}
}