I am developing a Download manager. And I want to implement progress bar, but it is not working(not Updating value) What can I do?
Please go through my code.
Thanks in Advance.
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
{
public Download()
{
InitializeComponent();
this.Loaded += OnLoad;
}
Uri uri;
static int row = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
}
private volatile bool _allowedToRun;
private string _source;
private string _destination;
private int _chunkSize;
Download d1 ;
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)
{
_allowedToRun = true;
_source = source;
_destination = destination;
_chunkSize = chunkSize;
_contentLength = new Lazy<int>(() => Convert.ToInt32(GetContentLength()));
BytesWritten = 0;
}
private long GetContentLength()
{
var request = (HttpWebRequest)WebRequest.Create(_source);
request.Method = "HEAD";
using (var response = request.GetResponse())
return response.ContentLength;
}
public event PropertyChangedEventHandler PropertyChanged;
private int _Progress;
public int Progress
{
get
{
return _Progress;
}
set
{
_Progress = value;
PropertyChanged(this, new PropertyChangedEventArgs("Progress"));
}
}
static public int p;
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());
p = per;
}
await fs.FlushAsync();
}
}
}
}
public Task Start()
{
_allowedToRun = true;
return Start( BytesWritten);
}
public void Pause1()
{
_allowedToRun = false;
}
void MakeButton2()
{
Button b2 = new Button();
b2.Click += new RoutedEventHandler(Onb2Click2);
}
public void Onb2Click2(object sender, RoutedEventArgs e)
{
pb.Value = p;
MessageBox.Show(p + "");
}
void OnLoad(object sender, RoutedEventArgs e)
{
MessageBox.Show(p + "");
}
public void change ( int i)
{
MessageBox.Show(i + "");
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
try
{
Onb2Click2(sender, e);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
}
What I have tried:
I have tried to direct update the value but it is throwing exception :-
object reference not set to an instance