WPF BackgroundWorker blocked application

  • Thread starter Thread starter Markus Freitag
  • Start date Start date
M

Markus Freitag

Guest
Hello,
I have a WPF application and a BackgroundThread / BackgroundWorker.
I press the button, the
MessageBox.Show("Ready");
appears,
then is blocked the UserInterface.

That's exactly what I'm trying to avoid with this thread.

How can I solve it? FTP Uploader

Do I have to bring in the dispatcher?
If yes, how and why?
My code.
Thanks in advance for tipps.
With best regards Markus

namespace FTPTest
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
FTPUpload MyFTP;
public MainWindow()
{
InitializeComponent();

MyFTP = new FTPUpload();
}

private void Start(object sender, RoutedEventArgs e)
{
//MyFTP.UploadFiles();
// Dispatcher.Invoke((Action)(() => MyFTP.UploadFolder()));

MyFTP.UploadFolder();
MessageBox.Show("Ready");
}

private void Cancel(object sender, RoutedEventArgs e)
{
MyFTP.Cancel();
}
}
}

// ----

public FTPUpload()
{
BackgroundWorkerFTP = new BackgroundWorker();
BackgroundWorkerFTP.DoWork += BackgroundWorkerFTP_DoWork;
BackgroundWorkerFTP.ProgressChanged += BackgroundWorkerFTP_ProgressChanged;
BackgroundWorkerFTP.RunWorkerCompleted += BackgroundWorkerFTP_RunWorkerCompleted;
BackgroundWorkerFTP.WorkerReportsProgress = true;
BackgroundWorkerFTP.WorkerSupportsCancellation = true;

InputParameter.Username = "XXXXX";
InputParameter.Password = "YYYYYYYY";
InputParameter.Server = "ftp://ftp.test.de"; // "127.0.0.1";
}

public void UploadFolder()
{
InputParameter.UploadFolder = "C:\\Temp";
InputParameter.CurrentUploadFile = "";

if (BackgroundWorkerFTP.IsBusy != true)
{
// Start the asynchronous operation.
BackgroundWorkerFTP.RunWorkerAsync(InputParameter);
}
}


private void BackgroundWorkerFTP_DoWork(object sender, DoWorkEventArgs e)
{
//Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
//{
// FTPUpload bwSender = (FTPUpload)sender;
//BackgroundWorker bw = (BackgroundWorker)sender;
//System.Diagnostics.Debug.Assert(this == bw);

var w = sender as BackgroundWorker;

string fileName = ((FtpSetting)e.Argument).FileName;
string fullName = ((FtpSetting)e.Argument).FullName;


string userName = ((FtpSetting)e.Argument).Username;
string password = ((FtpSetting)e.Argument).Password;
string server = ((FtpSetting)e.Argument).Server;
string uploadFolder = ((FtpSetting)e.Argument).UploadFolder;



string[] files = Directory.GetFiles(uploadFolder);
for (int iFile = 0; iFile < files.Length; iFile++)
{
if (!BackgroundWorkerFTP.CancellationPending)
{
FileInfo fi = new FileInfo(files[iFile]);

Application.Current.Dispatcher.Invoke((Action)(() =>
{

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", server, fi.Name)));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(userName, password);
request.Proxy = null;
Stream ftpStream = request.GetRequestStream();
FileStream fs = File.OpenRead(fi.FullName);
byte[] buffer = new byte[1024];
double total = (double)fs.Length;
int byteRead = 0;
double read = 0;

do
{
if (!BackgroundWorkerFTP.CancellationPending)
{
//Upload file & update process bar
byteRead = fs.Read(buffer, 0, 1024);
ftpStream.Write(buffer, 0, byteRead);
read += (double)byteRead;
double percentage = read / total * 100;

// bwSender.CurrentUploadFile = fi.FullName;
BackgroundWorkerFTP.ReportProgress((int)percentage, fi.FullName);
}
}
while (byteRead != 0);

// Using RunWorkerCompleted
e.Result = files.Length;

fs.Close();
ftpStream.Close();

}));
}
else
break;
}
//}));
}

Continue reading...
 
Back
Top