ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Check Gmail using C#

<< < (8/8)

kyrathaba:
Good points, all.  I was just doing a quick and dirty test of the UploadFile method is all.  In a full-blown application, I'd have the btnWhatever_Click handler hand off the task to a backgroundWorker object.

Maybe I'll create one and see if that fixes this issue.  Although, in the current case, why does DownloadFile work without hanging the UI, but UploadFile hangs it?  Hmm...

kyrathaba:
Okay, my button Click event now hands the file upload off to a backgroundWorker object running on its own thread:


--- Code: C# ---private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {            MessageBox.Show("Doing some work...");            string username = "my_username";            string password = "my_password";            string host = "ftp://glensforkumc.com";            string myLocal = Path.GetDirectoryName(Application.ExecutablePath) + "\\myTextFile.txt";            string myRemote = host + "/httpdocs/non_church/";            clsFTPclient client = new clsFTPclient(host + "/httpdocs/non_church/", username, password);            client.UploadFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\myTextFile.txt");         }         private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) {             MessageBox.Show("I'm done uploading");        }
The file does get uploaded, but my RunWorkerCompleted() messageBox never pops up... remove everything except the messageBox from the _DoWork() method above, and it's msgBox (as well as RunWorkerCompleted's msgBox) both fire as expected.

kyrathaba:
Interesting.  The second MessageBox never gets invoked.  At least, I never see it pop up when I run the program click my button to upload the file:


--- Code: C# ---public void UploadFile(string FullPathFilename) {            string filename = Path.GetFileName(FullPathFilename);             try {                 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + filename);                request.Method = WebRequestMethods.Ftp.UploadFile;                request.Credentials = new NetworkCredential(_remoteUser, _remotePass);                 StreamReader sourceStream = new StreamReader(FullPathFilename);                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());                 request.ContentLength = fileContents.Length;                 Stream requestStream = request.GetRequestStream();                requestStream.Write(fileContents, 0, fileContents.Length);                 MessageBox.Show("About to make upload request..."); //this one "pops-up", as expected                FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //does get invoked (my file DOES get uploaded)                MessageBox.Show("Made upload request.  About to close streams..."); //I never see this one                 response.Close();                requestStream.Close();                sourceStream.Close();             }            catch (Exception ex) {                                MessageBox.Show(ex.Message, "Upload error");            }            finally {             }         }

kyrathaba:
I'll be scratching my head over this one for a while, but it all works as expected now; no inexplicable hanging (even when doing the upload on its own asynch thread, it had 'seemed' to be hanging, in that I got a non-responsive UI):

kyrathaba:
I've updated my Snipplr posting here.

Navigation

[0] Message Index

[*] Previous page

Go to full version