I know there have been a lot of questions here but I have went through a ton of them and have had little luck. I'm new to events and background workers and I just don't know the best way to implement this.
I have a a basic Windows form in C#. It contains a progress bar. I am calling a class that is to download a file. I want the progress bar to update based on that download. I have it working fine if everything is all in the same class, but I can't get it to work in this situation. What is the best way to handle this and how might I go about it? Currently I'm doing this:
WebClient downloader = new WebClient();
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
And then for progress changed I do this:
public void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
pbDownload.Value = e.ProgressPercentage;
}
But when I put all of this except for the progress bar in a separate class, it gets all messed up. Ideas? Thanks!
I have this witch is about the same as what you are trying to do and works fine
FStatus ... form reference
FrmStatus FStatus = null;
public void ProgressInit(String caption, string opis, int Max)
{
FStatus = new SZOKZZ.FrmStatus();
FStatus.InitProc(Max);
FStatus.SetCaption(caption, opis);
FStatus.Show();
}
public void DLProgress(string curl, string cdl)
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DLDone);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DLSetProc);
webClient.DownloadFileAsync(new Uri(curl), cdl);
}
private void DLSetProc(object sender, DownloadProgressChangedEventArgs e)
{
this._FileProcentDownloaded = e.ProgressPercentage;
FStatus.SetProcDL(this._FileProcentDownloaded);
}
private void DLDone(object sender, AsyncCompletedEventArgs e)
{
_DlError = e.Error;
FStatus.Dispose();
FStatus = null;
}
You should call Application.DoEvents(); to force your form to update controls based on their new values :
public void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
pbDownload.Value = e.ProgressPercentage;
Application.DoEvents();
}
Regards
Related
I'm having an issue getting a progressbar to show download progress. The files are downloading without issue, but something is causing my progressbar to not update and I can't figure out why.
I've tried setting the progressBar value manually in the download and wc_DownloadProgressChanged method, but the only place it actually changes is the Form1_Load method.
using System;
using System.Windows.Forms;
using System.Threading;
namespace Launch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Downloader downloader = new Downloader();
ThreadStart job = new ThreadStart(downloader.download);
Thread thread = new Thread(job);
thread.Start();
}
private void ProgressBar_Click(object sender, EventArgs e)
{
}
public void SetProgress(int val)
{
progressBar.Value = val;
}
public void SetVisible(bool val)
{
progressBar.Visible = val;
}
}
}
using System;
using System.Data;
using System.Net;
using Newtonsoft.Json;
namespace Launch
{
class Downloader
{
public void download()
{
WebClient client = new WebClient();
string url = "https://someurl.com/manifest.json";
string json = client.DownloadString(url);
DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);
DataTable dataTable = dataSet.Tables["Required"];
foreach (DataRow row in dataTable.Rows)
{
string remoteUri = row["url"].ToString();
string fileName = row["name"].ToString();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFile(remoteUri, fileName);
Console.WriteLine("Did something with " + remoteUri);
}
}
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
var form = new Form1();
form.SetProgress(e.ProgressPercentage);
}
}
}
Would anyone be able to shed some light on what I'm doing wrong here?
EDIT:
I was able to get this working for the most part using DownloadFileAsync, but the progress bar was bouncing back and forth, I'm assuming because it's trying to calculate the progress for each individual file as bytes are received, so I'd like to get this working with DownloadFile.
The issue I'm now having with DownloadFile is that I have it running as a task but it's skipping all of the files (not downloading any of them, just prints them all out to console super fast).
Here's the code I'm using currently:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
string url = "https://someurl.com/manifest.json";
string json = client.DownloadString(url);
DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);
DataTable dataTable = dataSet.Tables["Required"];
foreach (DataRow row in dataTable.Rows)
{
string remoteUri = row["url"].ToString();
string fileName = row["name"].ToString();
Task.Run(() => {
client.DownloadFile(remoteUri, fileName);
});
Console.WriteLine("Did something with " + remoteUri);
}
}
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate {
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
label1.Text = "Downloaded ";
label2.Text = e.BytesReceived.ToString();
label3.Text = e.TotalBytesToReceive.ToString();
progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
});
}
Any ideas?
Use this code below :
private void Form1_Load(object sender, EventArgs e)
{
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadStringCompleted += client_DownloadStringCompleted;
Uri url = new Uri("url");
client.DownloadStringAsync(url);
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
SetVisible(false);
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
SetProgress(e.ProgressPercentage);
}
public void SetProgress(int val)
{
progressBar.Value = val;
}
public void SetVisible(bool val)
{
progressBar.Visible = val;
}
This likely should belong to https://codereview.stackexchange.com/
First of all, do not add a handler in a loop
client.DownloadProgressChanged += client_DownloadProgressChanged;
First time it will be fine, on 2nd item it will be called 2x times, on 3rd time it will be called 3 times, etc. You want to set handler once. Move it just after line:
WebClient client = new WebClient();
Second of all, each time a progress update is fired you are creating a new instance of the form. Create a private variable or property once.
private Form1 form = new Form1();
edit:
In case of UI, usually you can modify it only in the dispatcher thread that created it or use marshaling. I would just remove it as our already downloading the string async.
Also, I would not use e.ProgressPercentage, but something like:
Math.Truncate(e.BytesReceived / (double)e.TotalBytesToReceive * 100)
My app for Windows Phone has a string downloaded from server. I'm trying displaying a textBlock while the download is in execution. However, my code doesn't works.
The textBlock is not displayed. Why? Is there another way for this?
WebClient webClient1 = new WebClient();
webClient1.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
webClient1.DownloadStringCompleted += webClient_DownloadStringCompleted1;
webClient1.DownloadStringAsync(new Uri(string.Format(url + insCodComanda.Text + "&random=" + ran.Next().ToString(), UriKind.RelativeOrAbsolute)));
public void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
testete.Visibility = Visibility.Visible;
}
void webClient_DownloadStringCompleted1(object sender, DownloadStringCompletedEventArgs e)
{
testete.Visibility = Visibility.Collapsed;
}
If you want to manipulate UI element like that you need to use Dispatcher
this.Dispatcher.BeginInvoke(new Action(() => testete.Visibility = Visibility.Visible));
I have wcf servise that Update db it is takes 10-15 sec,and i wont to run/show my form with loading/waitting statusbar while servise working, and when service is finished i need to close the watting form.
My problem is when i run ShowDialog(); it is get stuck on it , and don't go to my service.
What i doing wrong here?
My code
My function
public static void UpdateSNXRATES(object sender, EventArgs e)
{
WaitForm waitF = new WaitForm();
waitF.ShowDialog();//here it stuck
using (var Server = new ServiceReference.Service1Client())
{
Server.ClientCredentials.Windows.ClientCredential.Domain = strDomain;
Server.ClientCredentials.Windows.ClientCredential.UserName = strUser;
Server.ClientCredentials.Windows.ClientCredential.Password = strPassword;
success=Server.UpdateSNXRATES();
}
waitF.Close();
}
My WaitForm code
public partial class WaitForm : Form
{
public WaitForm()
{
InitializeComponent();
}
private void WaitForm_Load(object sender, EventArgs e)
{
radWaitingBar1.StartWaiting();
radWaitingBar1.WaitingSpeed = 100;
radWaitingBar1.WaitingStep = 5;
}
}
ShowDialog() is a blocking call, i.e. the current thread will keep waiting on this line until the form is closed (by the user). You should show your WaitForm on a different thread than the main application thread, combined with Invoke() call to ensure that you don't do illegal cross-thread operations. You can use BackgroundWorker component to load and show your WaitForm on a different thread.
Alternately and preferably, you should move your service initialization and running code to the BackgroundWorker. That will ensure you don't need any Invokes.
Example
ServiceReference.Service1Client Server;
WaitForm waitF;
public static void UpdateSNXRATES(object sender, EventArgs e)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
bw.RunWorkerAsync();
waitF = new WaitForm();
waitF.ShowDialog();
}
static void bw_DoWork(object sender, DoWorkEventArgs e)
{
Server = new ServiceReference.Service1Client();
Server.ClientCredentials.Windows.ClientCredential.Domain = strDomain;
Server.ClientCredentials.Windows.ClientCredential.UserName = strUser;
Server.ClientCredentials.Windows.ClientCredential.Password = strPassword;
success = Server.UpdateSNXRATES();
}
static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
waitF.Close()
}
How can I stop my application from not displaying until the form_load code is completed?
public partial class updater : Form
{
public updater()
{
InitializeComponent();
timer1.Interval = (10000) * (1);
progressBar1.Value = 0;
progressBar1.Maximum = 100;
progressBar1.Update();
timer1.Start();
}
private void updater_Load(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;
webClient.DownloadFile("http://download827.mediafire.com/jl9c098fnedg/ncqun56uddq0y1d/Stephen+Swartz+-+Survivor+%28Feat+Chloe+Angelides%29.wav", Application.StartupPath + "\\Stephen Swartz - Survivor (Feat Chloe Angelides).wav");
// System.Diagnostics.Process.Start("\\Test.exe");
this.Close();
}
void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressBar1.Update();
}
}
If you use DownloadFileAsync it wont block the UI thread and will allow the Form to load and show the progress in the Progressbar, then you can use the DownloadFileCompleted event to close the Form
Example:
public Form1()
{
InitializeComponent();
progressBar1.Value = 0;
progressBar1.Maximum = 100;
progressBar1.Update();
}
private void updater_Load(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
webClient.DownloadFileAsync(new Uri("http://download827.mediafire.com/jl9c098fnedg/ncqun56uddq0y1d/Stephen+Swartz+-+Survivor+%28Feat+Chloe+Angelides%29.wav"), Application.StartupPath + "\\Stephen Swartz - Survivor (Feat Chloe Angelides).wav");
}
private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
Close();
}
private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressBar1.Update();
}
One way is to move your code from Load Shown Event. So the code will start runing after the form is shown.
The other is create a thread, where you will download a file.
For this purpose you can use BackgroundWorker
private void updater_Load(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, eArgs) =>
{
WebClient webClient = new WebClient();
webClient.DownloadFile("someUrl", "somePath");
};
worker.RunWorkerAsync();
}
Also there exists webClient.DownloadFileAsync method witch suits better in this situation. You can find description in sa_ddam213 answer.
I need to change my backgroundworker progress outside the DoWork Event is this possible ?
My code
private void progress_changed(string fileName, DownloadProgressChangedEventArgs e)
{
progress = e.ProgressPercentage;
Filename = fileName;
// change the progress here
}
private void worker_DoWork(string fileName)
{
WebClient client = new WebClient();
client.DownloadProgressChanged += (obj, e) => progress_changed(fileName, e);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(progress_complete);
client.DownloadFileAsync(new Uri(Link), Savepath);
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
mainForm.dlist.Items[mainForm.dlist.Items.IndexOfKey(Filename)].SubItems[2].Text = progress.ToString() + "%";
mainForm.dlist.Items[mainForm.dlist.Items.IndexOfKey(Filename)].SubItems[3].Text = "Downloading";
}
Pass the BackgroundWorker to the other function. From there, you can call ReportProgress. Alternatively, depending on your structure, you could make the BackgroundWorker a class field and access it that way.