How to update progress bar value in WPF from backend?
Nehal Chaudhari
41
Reputation points
I am developing a download manager, and I want to implement a progress bar, I have added that and it is working fine with webclient but now I am using HttpWebRequest and I am updating progress bar value but it is not updating sometimes it throws an exception "object reference not set to an instance" What can I do?
I am attaching my code Please Help!!
Thank 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;
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;
namespace DownloadManager
{
/// <summary>
/// Interaction logic for Download.xaml
/// </summary>
public partial class Download : Window
{
public Download()
{
InitializeComponent();
}
// Download d1 = new Download();
public Download(string url, string p)
{
Thread thread = new Thread(() => download(url,p));
thread.Start();
}
Uri uri;
static WebClient client = new WebClient();
static int row = 0;
double Percentage;
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 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;
}
public int p0=10;
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"));
}
}
private async Task Start(object sender, 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())
{
d1 = new Download();
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;
// a1 = int.Parse(Math.Truncate(Percentage).ToString());
//pb.Value = int.Parse(Math.Truncate(Percentage).ToString());
//progress(int.Parse(Math.Truncate(Percentage).ToString()));
// MessageBox.Show(int.Parse(Math.Truncate(Percentage).ToString()) + "Done");
int per = int.Parse(Math.Truncate(Percentage).ToString());
Download d = new Download();
try
{
d.p0 = per;
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
await fs.FlushAsync();
}
}
}
}
public Task Start()
{
_allowedToRun = true;
Download download = new Download();
return Start( download,BytesWritten);
}
public void Pause1()
{
_allowedToRun = false;
}
public void change (object sender, RoutedEventArgs e)
{
pb.Value = p0;
// Pause_Click(sender, e);
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
//change();
}
}
}
Developer technologies Windows Presentation Foundation
2,854 questions
Developer technologies XAML
859 questions
Developer technologies C#
11,568 questions
Sign in to answer