Batch Uploading

Topics: Developer Forum
Jul 7, 2009 at 9:50 PM

I'm trying to create a batch uploader and I'm encountering some odd behavior.

My uploader processes a directories worth of pictures one at a time.  I have an event handler that's subscribed to the OnUploadProgress event and that's being called twice (once for each of the first two photos), but the third upload never calls the event handler and I'm not getting an error message.  The application just seems to hang.

Here's the code that initiates an upload

private void UploadFile(int listIdx) {
  string imgID = lvFiles.Items[listIdx].Text;
  string filename = txtFolder.Text + @"\" + lvFiles.Items[listIdx].SubItems[1].Text;
  progressImage.Value = 0;
  curFileSize = new FileInfo(filename).Length;
  string tag = gTags + ", ENTRANT-" + imgID;
  Main.f.UploadPicture(filename, "Entrant " + lvFiles.Items[listIdx].Text, "XXXXt", tag);
} // UploadFile()

...and here's the event handler code...

void f_OnUploadProgress(object sender, UploadProgressEventArgs e) {
  //throw new NotImplementedException();
  Application.DoEvents();
  if (e.UploadComplete) {
    progressBatch.Value++;
    progressImage.Value = 0;
    nextFile++;
    UploadFile(nextFile);
  } else {
    progressImage.Value = (e.Bytes / curFileSize) * 100;
  }
} // f_OnUploadProgress();

Any ideas why the third file upload never completes? Is there something I need to be doing between calls to the UploadPicture() method?

Coordinator
Jul 8, 2009 at 9:08 AM

No idea off the top of my head. I'll try and get around to giving your code a go and see if I can work it out.

One thing I notice is that there is nothing in the code to stop the next file being uploaded - even if there is no next file. I assume you are staring the whole thing off with a call to UploadPicture(0)?

Jul 8, 2009 at 3:29 PM

Correct, I'm not currently doing bounds checking on the ListView from which I'm reading entries.  That will eventually be added, but the directory in question has 44 files in it, so I know it's not an out of bounds issue, which would raise an exception in any case.  And yes, UploadPicture(0) is called inside of another function to kick the whole process off.  Please do let me know if anything occurs to you, or if you're able to test the code yourself; I'll be digging into this again today myself.  If I figure out what's causing the problem, I'll definitely post back here with the solution.  Thanks for your response.

Jul 8, 2009 at 4:18 PM
Edited Jul 8, 2009 at 4:41 PM

So, I did a bit of testing, and here's my current function...

 

 

    private void UploadFile(int listIdx) {
      if (lvFiles.Items.Count > listIdx) {
        string flickr_id = "";
        string imgID = lvFiles.Items[listIdx].Text;
        string filename = txtFolder.Text + @"\" + lvFiles.Items[listIdx].SubItems[1].Text;
        progressImage.Value = 0;
        curFileSize = new FileInfo(filename).Length;
        string tag = gTags + ", ENTRANT-" + imgID;
        FlickrNet.Uploader u = new Uploader();
        try {
          flickr_id = Main.f.UploadPicture(filename, "Entrant " + lvFiles.Items[listIdx].Text, "XXXXXX", tag);
        } catch (Exception ex) {
          MessageBox.Show("Error: " + ex.Message);
        }
        dbg += String.Format("{0} : {1}\n", flickr_id, filename);
        MessageBox.Show(dbg);
      } else {
        MessageBox.Show("Attempt to upload file with invalid index: " + listIdx, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    } // UploadFile()

 

 

What's strange, is that, as I mentioned, the 3rd upload is where the app hangs, but while stepping through this, each of the first two calls to UploadPicture() don't seem to return.  None of the code following the call to UploadPicture executes.  I'm neither seeing a message box for a caught exception, nor am I seeing the debug message (dbg) that I'm trying to display.  The code appears to continue running, but seems to somehow drop out of UploadFile() without any further execution.  So what I'm seeing is...

  1. Initial call to UploadFile.  e.g. UploadFile(0)
  2. Execution in UploadFile up to the call to UploadPicture()
  3. Execution at this point seems to drop out of UploadFile() without executing any code following the call to UploadPicture()
  4. The OnUploadProgress event fires for both of the first two files and the event handler makes progressive calls to UploadPicture, incrementing the index each time.
  5. On the third call to UploadFile, where 2 is passed as the argument, code executes up to UploadPicture() and then the entire application hangs.

Very strange.  Just for fun I think I'll restart VisualStudio (and possibly all of Windows) to make sure there's not something weird with the underlying environment.

Jul 8, 2009 at 5:12 PM

So, I think I've got this resolved.  What I saw happening was...

  1. UploadPicture call made.
  2. UploadPicture fires an OnUploadProgress event
  3. UploadPicture called from within the OnUploadProgress event handler.

So it would seem that I was effectively making recursive calls to UploadPicture.  I placed the calls to UploadPicture inside of a loop (where I was originally calling UploadPicture(0)) so new calls aren't made until the old ones return and that seems to have resolved the issue, although the OnUploadProgress event doesn't seem to fire very often and effectively blocks the UI since I'm calling from inside the UI's thread.  I'm not sure if there's a way to make FlickrNet give more frequent status updates, or if I need to run this upload process on a separate thread to keep the UI responsive, but at least I have batch uploading working now.

samjudson, thanks again for getting involved.

 

 

Coordinator
Jul 8, 2009 at 6:54 PM

Well I would definitely put the work on a BackgroundWorker thread, and post the progress back to the UI thread.

Don't feel like I was much help but glad you got it sorted :)