|
[UPDATE] - Sorry, the formatting is all screwy.
First, it's been a while since I did any WinForms, and this isn't my app, sooo...
I need to call 2 methods, both of which download a file off a device. I want to first pop up a dialog with 2 progress bars and update them as the file is downloaded.
I can't get the dialog to refresh. The Progress bar's value IS being set. I just don't see any change.
So far I have:
private void DownloadDeviceFiles()
{
DownloadDialog dialog = new DownloadDialog();
dialog.Text = "Retrieving Mission Data";
dialog.Progress1Caption = "Config File";
dialog.Progress2Caption = "Database";
dialog.StartPosition = FormStartPosition.CenterScreen;
dialog.Show(this);
DownloadConfigFile(dialog);
.
.
.
.
}
Then in the DownloadConfigFile method I have:
private bool DownloadConfigFile(DownloadDialog dialog)
{
int percentDone = 0;
int speed = 0;
int downloadResponse = 0;
int cause = 0;
int totalTime = 0;
int attempts = 0;
bool isDownloaded = false;
bool isFileRead = false;
string message = string.Empty;
<pre>
while (attempts < 3)
{
// If the file has not yet been downloaded...
if (!isDownloaded)
{
// Download the file from the device to the local folder
ClientComm.GetFile(FileSystemLocations.TemoraryConfigurationFileLocation, null, (int)ClientComm.file_types.file_user_config, 0, 0);
// Get the file
string fileData = "";
while (true)
{
Thread.Sleep(10);
totalTime += 10;
MDF.ClientComm.FileProgress(out fileData, out percentDone, out speed, out downloadResponse, out cause);
dialog.Progress1PercentDone = percentDone;
if ((percentDone == 100) || (totalTime < MAX_CONFIG_DOWNLOAD_TIME) || dialog.Cancelled)
{
break;
}
}
}
}
return ConfigFileDownloaded;
}
Again, the problem is that the dialog does not refresh. What am I doing wrong here?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Did you make sure that in DownloadDialog.Progress1PercentDone the progress is actually being set?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
As I said I my post-the value IS being set
If it's not broken, fix it until it is
|
|
|
|
|
Coder (Hired) wrote: As I said I my post-the value IS being set Sorry, I'm tired.
You should run your download-method in a background-worker * . When running it on the UI-thread, the UI-thread can't repaint the progress bar.
At least not reliably. Actually I'm a bit confused myself now. I would have assumed repainting wouldn't happen at all until your download-method finishes. But I just set up a quick test-project and the progress bar-repainting did work "partially" (stuttering) while the UI thread was executing the setprogress-sleep-loop. Maybe someone else coming along here can shed some light on that.
However, running lengthy processes in a background worker * and not on the UI-thread is advisable in any case to keep your UI responsive and will definitely solve your issue.
* or some other multithreading-flavor
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Ok, but how does the dialog fit into the BG worker? The dialog is in the UI thread. how does the worker notify it that progress changed?
If it's not broken, fix it until it is
|
|
|
|
|
|
One thing to consider is that the app cannot continue if the 2 files are not downloaded, so there's no reason why this can't be a blocking call
If it's not broken, fix it until it is
|
|
|
|
|
The "blocking nature" would have to be implemented by showing DownloadDialog modal (by calling .ShowDialog() instead of .Show()).
You can indicate success/failure along with any other result values of the download process with the Result-Property of DoWorkEventArgs and then proceed accordingly.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Why not use a MethodInvoker that wraps the download code. I could pass that to the dialog, then in the Shown event I could fire it off.
The only question then is how does that code update the UI?
Something like this:
private MethodInvoker GetConfigFileDownloadInvoker()
{
MethodInvoker invoker = new MethodInvoker(delegate
{
});
return invoker;
}
private MethodInvoker GetDatabaseDownloadInvoker()
{
MethodInvoker invoker = new MethodInvoker(delegate
{
});
return invoker;
}
private void DownloadDeviceFiles()
{
DatabaseDownloaded = false;
ConfigFileDownloaded = false;
MethodInvoker invoker1 = GetConfigFileDownloadInvoker();
MethodInvoker invoker2 = GetDatabaseDownloadInvoker();
dialog = new DownloadDialog(invoker1, invoker2);
dialog.Text = "Retrieving Mission Data";
dialog.Progress1Caption = "Config File";
dialog.Progress2Caption = "Database";
dialog.StartPosition = FormStartPosition.CenterScreen;
dialog.ShowDialog(this);
if (!ConfigFileDownloaded || !DatabaseDownloaded)
{
OnDisconnected(EventArgs.Empty);
}
}
If it's not broken, fix it until it is
modified 29-Apr-15 17:32pm.
|
|
|
|
|
The MethodInvokers don't really gain you anything - they'd just move the point where you start the download-method from your parent form to your download dialog. To achieve a responsive UI you'd still need some kind of dedicated download thread.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Ok, I can see how a background worker would work. I guess I'll have to use that
If it's not broken, fix it until it is
|
|
|
|
|
Ok, so I added a BW, and in the I now have:
private void configWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (InvokeRequired)
{
var del = new configWorker_DoWorkDelegate(configWorker_DoWork);
Invoke(del, new object[] { this, e });
}
else
{
dialog.ShowDialog(this);
DownloadConfigFile();
}
}
so now it sits there until I close the dialog.
I'm not getting this. Could use some help.
Remmeber, this needs to block until BOTH files are downloaded.
If it's not broken, fix it until it is
|
|
|
|
|
Don't put any UI stuff into the BW. It should look something like the following. Not tested, obviously, and it's a bit hacky. It would actually make sense to start the BW from the DownloadDialog in order to ensure that the DownloadDialog "exists" when ProgressChanged and RunWorkerCompleted fire.
class Form1 : Form
{
BackgroundWorker worker;
DownloadDialog downloadDialog;
public Form1()
{
InitializeComponent();
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.ProgressChanged += worker_ProgressChanged;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
void DownloadStartButton_Click(object sender, EventArgs e)
{
DownloadDeviceFiles();
}
void DownloadDeviceFiles()
{
worker.RunWorkerAsync();
DownloadDialog downloadDialog = new DownloadDialog();
downloadDialog.Text = "Retrieving Mission Data";
downloadDialog.Progress1Caption = "Config File";
downloadDialog.Progress2Caption = "Database";
downloadDialog.StartPosition = FormStartPosition.CenterScreen;
downloadDialog.ShowDialog(this);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = DownloadConfigFile() && DownloadDatabase();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (downloadDialog != null)
downloadDialog.Progress1PercentDone = e.ProgressPercentage;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
downloadDialog.Close();
downloadDialog.Dispose();
if (e.Result == true)
else
}
bool DownloadConfigFile()
{
while (true)
{
worker.ReportProgress(percentDone);
}
return downloadSuccess;
}
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
100% right on!
THANK YOU! THANK YOU! THANK YOU!
If it's not broken, fix it until it is
|
|
|
|
|
You're welcome
(The invoice is on its way )
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Hy, I'm new to CodeProject.
I'm trying to set text to label.
When I assign text "Lahore is the capital city of the Pakistani province of Punjab, the second largest metropolitan area in the country and an important historical center in South Asia.", It does not display full text.
I have a label with size(200, 70), font(Arial, 15pt).
I want to assign only the text which fits on label (using defined size and font, that does not dsplay (...) at the end) and save the remaining text in a string variable.
Please Help.
|
|
|
|
|
To do that, you would have to measure the string and work out from that how much of it will fit.
See here: Graphics.MeasureString[^] but to use it, you need to get a Graphics context for the label (which you are responsible for Disposing when you are finished with it): Control.CreateGraphics(v=vs.110).aspx[^]
Don't expect this to be trivial!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Can you please gave me an example???
|
|
|
|
|
Why don't you give it a try yourself first? For me figuring new stuff out myself is one of the most rewarding aspects of being a software developer. There will probably also be basic examples on those pages OriginalGriff linked for you or you can find some by googling for it.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
You have all the information you need to do your own research now.
|
|
|
|
|
The only example I could give you - other than that in the two links - is basically to do it all for you, and that isn't what we are here for!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
sir, i want to resize the controls like label,picturebox,textbox,barcode,etc proportionally in c#...
thanks in advance..
|
|
|
|
|
|
Where's the question part? How to do so? There are some examples out there, listed on Google.
I'd recommend against it though; it isn't very helpful to have a 640x480 form resize to fill the entire 1024x768.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: it isn't very helpful to have a 640x480 form resize to fill the entire 1024x768.
It's very Metro though!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|