I got a problem with this code. I was downloading a file using following code and when I minimized the visual studio window, I got a null reference expectation error.
Object reference not set to an instance of an object
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace Downloader
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = "c:\\" + GetFileName(textBox1.Text);
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri(textBox1.Text),textBox2.Text);
}
private string GetFileName(string hrefLink)
{
string[] parts = hrefLink.Split('/');
string fileName = "";
if (parts.Length > 0)
fileName = parts[parts.Length - 1];
else
fileName = hrefLink;
return fileName;
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Form1.ActiveForm.Text = "Downloader" + " " + e.ProgressPercentage.ToString() + "%";
label2.Text = e.BytesReceived.ToString() + "/" + e.TotalBytesToReceive.ToString();
progress.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
}
}
It happens whenever I try to close or open another app like mozilla during the run time of this code.
There's another problem with release version but first I want this shit solved.
So, how do I take care of this error.
Thanks in advance!!

New Topic/Question
Reply



MultiQuote




|