I am very confused over WPF , C# and allowing the GUI to update.
I'm trying to create an FTP helper class based on :
I want to pop open a progress window with a progress bar to show the progress of the FTP.
The problem as I understand WPF is if you start a long running process on the GUI thread then any attempted update to the GUI will not update, so you have to use a worker process.
All examples I have found for a progress update uses an asynchronous worker process, but that doesn't make sense.
How can I have a method that returns Boolean as to the success of the FTP if the worker process called is Async?
I don't want to tie up the GUI/UI thread but I also don't want the return to the client from the method call to happen until the process either succeeded or failed.
here is what I have...
I haven't fully implemented the FTP code yet, but am stuck with understanding how I run a background worker, keeping the UI thread free so the progress bar works, but not retuning from the FTP.Put() method until the background work has finished?
I cannot see a "RunWorkerSynch" method, to run it synchronously, so am I using the wrong type of worker thread?
your help is appreciated.
1DMF.
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
I'm trying to create an FTP helper class based on :
I want to pop open a progress window with a progress bar to show the progress of the FTP.
The problem as I understand WPF is if you start a long running process on the GUI thread then any attempted update to the GUI will not update, so you have to use a worker process.
All examples I have found for a progress update uses an asynchronous worker process, but that doesn't make sense.
How can I have a method that returns Boolean as to the success of the FTP if the worker process called is Async?
I don't want to tie up the GUI/UI thread but I also don't want the return to the client from the method call to happen until the process either succeeded or failed.
here is what I have...
Code:
/// <summary>
/// Interaction logic for ProgressBar.xaml
/// </summary>
public partial class ProgressBar : Window
{
public ProgressBar()
{
InitializeComponent();
}
}
public class FTP
{
// private attributes
private ProgressBar pgbar;
private string HostName { get; set; }
private string UserName { get; set; }
private string Password { get; set; }
private string LocalFile { get; set; }
private string RemoteFile { get; set; }
private string RemoteDir { get; set; }
private int counter { get; set; }
private int total { get; set; }
// public properties
public bool OK { get; private set; }
// Constructor
public FTP() : this(new ProgressBar())
{
}
// Constructor with own ProgressBar
public FTP(ProgressBar pgb)
{
this.pgbar = pgb;
this.pgbar.ShowDialog();
}
// download a file
public bool Get(string hostname, string username, string password, string localfilename, string remotefilename,
string remotedirectory, int counter = 1, int total = 1)
{
// set attributes for worker process
this.counter = counter;
this.total = total;
this.HostName = hostname;
this.UserName = username;
this.Password = password;
this.LocalFile = localfilename;
this.RemoteFile = remotefilename;
this.RemoteDir = remotedirectory;
// init progress
this.InitProgress();
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += FTP_Get;
worker.ProgressChanged += UpdateProgress;
[COLOR=#FCE94F] worker.RunWorkerAsync();
// STUCK HERE -
return this.OK;
[/color]
}
private void InitProgress()
{
this.pgbar.Desc.Content = "Downloading " + this.counter + " of " + this.total + " (" + this.RemoteFile + ")";
this.pgbar.Progress.Value = 0;
this.pgbar.Perc.Text = "0%";
}
// FTP download helper
private void FTP_Get(object sender, DoWorkEventArgs e)
{
using (FtpClient conn = new FtpClient())
{
conn.Host = this.HostName;
conn.Credentials = new NetworkCredential(this.UserName, this.Password);
conn.DataConnectionEncryption = true;
using (Stream istream = conn.OpenRead(Path.Combine(this.RemoteDir, this.RemoteFile)))
{
try
{
// istream.Position is incremented accordingly to the reads you perform
// istream.Length == file size if the server supports getting the file size
// also note that file size for the same file can vary between ASCII and Binary
// modes and some servers won't even give a file size for ASCII files! It is
// recommended that you stick with Binary and worry about character encodings
// on your end of the connection.
}
finally
{
istream.Close();
}
}
}
}
}
}
I haven't fully implemented the FTP code yet, but am stuck with understanding how I run a background worker, keeping the UI thread free so the progress bar works, but not retuning from the FTP.Put() method until the background work has finished?
I cannot see a "RunWorkerSynch" method, to run it synchronously, so am I using the wrong type of worker thread?
your help is appreciated.
1DMF.
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music