7

I have a WinForms application in which my background worker is doing a sync task, adding new files, removing old ones etc.

In my background worker code I want to show a custom form to user telling him what will be deleted and what will be added if he continues, with YES/NO buttons to get his feedback.

I was wondering if it is ok to do something like this in background worker's doWork method? If not, how should I do it?

Please advise..

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   MyForm f = new MyForm();
   f.FilesToAddDelete(..);
   DialogResult result = f.ShowDialog();
   if(No...)
   return;
   else
   //keep working...
}
Jon
  • 428,835
  • 81
  • 738
  • 806
Ahmed
  • 14,503
  • 22
  • 92
  • 150

4 Answers4

9

If you try this you will see for yourself that it will not work because the BackgroundWorker thread is not STA (it comes from the managed thread pool).

The essence of the matter is that you cannot show user interface from a worker thread¹, so you must work around it. You should pass a reference to a UI element of your application (the main form would be a good choice) and then use Invoke to marshal a request for user interaction to your UI thread. A barebones example:

class MainForm
{

    // all other members here

    public bool AskForConfirmation()
    {
        var confirmationForm = new ConfirmationForm();
        return confirmationForm.ShowDialog() == DialogResult.Yes;
    }
}

And the background worker would do this:

// I assume that mainForm has been passed somehow to BackgroundWorker
var result = (bool)mainForm.Invoke(mainForm.AskForConfirmation);
if (result) { ... }

¹ Technically, you cannot show user interface from a thread that is not STA. If you create a worker thread yourself you can choose to make it STA anyway, but if it comes from the thread pool there is no such possibility.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks Jon I will try it out and see if it works for me.. Your answer has made sense.. – Ahmed May 08 '12 at 12:36
  • By the way, someone told me elsewhere it is safe to call MessageBox.show(..) on BackgroundWorker perhaps because the method is statis.. is it also unsafe() ? – Ahmed May 08 '12 at 12:38
  • @Ahmed: That someone doesn't know what they are talking about. There is a related question here, see http://stackoverflow.com/questions/559252/does-messagebox-show-automatically-marshall-to-the-ui-thread. Conclusion: you *can* use it, but not because it's `static` or some other rubbish reason like that. It's because it specifically sets up its own message pump. – Jon May 08 '12 at 12:42
  • Thank you Jon, You are the real Guru :) Please check this post which I was referring to about MessageBox where it was reasonsed that it is static no message pump was pointed. http://stackoverflow.com/questions/10283881/messagebox-on-worker-thread – Ahmed May 08 '12 at 12:52
  • @Ahmed: Well, that answer is indeed not phrased in the most technically accurate manner. I assume it came out like that because of imperfect communication between the person who wrote it (who undoubtedly knows *why* it works) and you. – Jon May 08 '12 at 12:56
  • Thanks Jon for being so helpful :) – Ahmed May 08 '12 at 13:01
4

I usually create a method to execute a delegate on the UI thread:

  private void DoOnUIThread(MethodInvoker d) {
     if (this.InvokeRequired) { this.Invoke(d); } else { d(); }
  }

With this, you can change your code to:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   DialogResult result = DialogResult.No;
   DoOnUIThread(delegate() {
      MyForm f = new MyForm();
      f.FilesToAddDelete(..);
      result = f.ShowDialog();
   });

   if(No...)
   return;
   else
   //keep working...
}
C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67
2

IMO answers stating that you should launch a thread to handle this are misguided. What you need is to jump the window back to the main dispatcher thread.

In WPF

public ShellViewModel(
    [NotNull] IWindowManager windows, 
    [NotNull] IWindsorContainer container)
{
    if (windows == null) throw new ArgumentNullException("windows");
    if (container == null) throw new ArgumentNullException("container");
    _windows = windows;
    _container = container;
    UIDispatcher = Dispatcher.CurrentDispatcher; // not for WinForms
}

public Dispatcher UIDispatcher { get; private set; }

and then, when some event occurs on another thread (thread pool thread in this case):

public void Consume(ImageFound message)
{
    var model = _container.Resolve<ChoiceViewModel>();
    model.ForImage(message);
    UIDispatcher.BeginInvoke(new Action(() => _windows.ShowWindow(model)));
}

WinForms equivalent

Don't set UIDispatcher to anything, then you can do have:

public void Consume(ImageFound message)
{
    var model = _container.Resolve<ChoiceViewModel>();
    model.ForImage(message);
    this.Invoke( () => _windows.ShowWindow(model) );
}

DRYing it up for WPF:

Man, so much code...

public interface ThreadedViewModel
    : IConsumer
{
    /// <summary>
    /// Gets the UI-thread dispatcher
    /// </summary>
    Dispatcher UIDispatcher { get; }
}

public static class ThreadedViewModelEx
{
    public static void BeginInvoke([NotNull] this ThreadedViewModel viewModel, [NotNull] Action action)
    {
        if (viewModel == null) throw new ArgumentNullException("viewModel");
        if (action == null) throw new ArgumentNullException("action");
        if (viewModel.UIDispatcher.CheckAccess()) action();
        else viewModel.UIDispatcher.BeginInvoke(action);
    }
}

and in the view model:

    public void Consume(ImageFound message)
    {
        var model = _container.Resolve<ChoiceViewModel>();
        model.ForImage(message);
        this.BeginInvoke(() => _windows.ShowWindow(model));
    }

Hope it helps.

Henrik
  • 9,714
  • 5
  • 53
  • 87
1

You should bring up the dialog before you run the backgroundworker. And in the progresschanged-event, you can update the dialog.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
Tomtom
  • 9,087
  • 7
  • 52
  • 95