0

Im trying to upload some images using the Flickr.net API.The Images are uploaded but the User Interface freezes.I have inserted the code for uploading in a Background worker

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    foreach (var item in imagelist)
    {
        flickr.UploadPicture(item, Path.GetFileName(item), null, null, true, false, true);
    }
    MessageBox.Show("Success");
}

The flickr object is created earlier from another form and passed to this form. I call the worker with if(worker.IsBusy==false){backgroundWorker1.RunWorkerAsync();} when a button is clicked.

George Brighton
  • 5,131
  • 9
  • 27
  • 36
techno
  • 6,100
  • 16
  • 86
  • 192
  • Are you binding to any events on the `flickr` object? For example, `UploadCompleted`, if available. – Marcel N. Jun 30 '12 at 08:02
  • @thecoon No im not binding to any events i just check if the loop is completed – techno Jun 30 '12 at 08:03
  • And how do you do that? You should wait for the background worker to be completed, via the `RunWorkerCompleted` event. – Marcel N. Jun 30 '12 at 08:04
  • You see in the bg worker there is loop(for each) after the code for the loop i present a message box saying "success" – techno Jun 30 '12 at 08:06
  • It's difficult to figure out the cause of the hang with the code supplied, can you add other code relevant to the background worker? – Farnk Jun 30 '12 at 08:14
  • Where is the code to display the message box? – Farnk Jun 30 '12 at 08:19

2 Answers2

2

Two common causes for this, your snippet is way too brief to narrow down which it might be. First is the ReportProgress method, the event handler runs on the UI thread. If you call it too often then the UI thread can get flooded with invoke requests and spend too much time to handle them. It doesn't get around to doing its regular duties anymore, like responding to paint requests and processing user input. Because as soon as it is done handling a invoke request, there's another one waiting to get dispatched. The UI thread isn't actually frozen, it just looks like it is. The net effect is the same. You'll need to fix it by slowing down the worker or call ReportProgress less often.

The second cause is your flicker object not being thread-safe and itself ensuring that it is used in a thread-safe way. By marshaling the call from the worker thread to the UI thread automatically. This is very common for COM components, this kind of marshaling is a core feature of COM. Again the UI thread isn't actually frozen, but it still won't handle paint and input since it is busy uploading a photo. You'll need to fix it by creating the flicker object on the worker thread. With good odds that you can't do this with a BackgroundWorker, such a component often needs an STA thread that pumps a message loop. Which requires Thread.SetApartmentState() and Application.Run().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • The Likely cause is the Second One.I do not bind to any events of the Background Worker except Dowork and i don;t use ReportProgress.Im not an Expert in Threads.Can you please give me some more Explanation to do solve the second problem.Thank You – techno Jun 30 '12 at 08:50
  • http://stackoverflow.com/questions/4269800/webbrowser-control-in-a-new-thread/4271581#4271581 – Hans Passant Jun 30 '12 at 08:53
  • Why can't i create a Flickr object in the Background worker Thread? – techno Jun 30 '12 at 08:55
  • Have you tried? You are asking me questions about a class I never heard of without documenting it. – Hans Passant Jun 30 '12 at 08:58
  • I have tried creating the flickr object in the Background Worker.The Images are uploaded but the UI is Still frozen.I have declared the Flickr object in the starting of the class and created a new object in the bgworker.What should i do.Please give some advice – techno Jun 30 '12 at 10:10
  • The UI Freezing problem is solved when i invoke the Form which shows the upload section from the Mainform by using Uploadform.show(); rather than Uploadform.showdialog(); But this has resulted in one new problem.After uploading is complete and i close the upload form.The mainform seems to be stuck for a 1 or 2 seconds.Why is this?Please Help me – techno Jun 30 '12 at 11:18
0

If you are doing something like:

while(worker.IsBusy)
{
}

to wait for it to finish, this will hang because it ties up the UI thread in the loop and since the background worker needs to invoke onto the UI thread to set the busy property safely there is a dead lock.

Farnk
  • 244
  • 3
  • 12