0

In a C# winform app, I have a background worker in the doWork method deep down the stack tree an object is calling

            // This is ok to be called on non UI thread because 
            // MessageBox has its own message pump
            result = MessageBox.Show(form, message, AppStrings.low_space_title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

The comments are from a previous developer, and this code apparently worked, now it seems to be not working any more.. the error that I am getting is

   Cross-thread operation not valid: Control 'FormMain' accessed from a thread other than the thread it was created on.

Any help would be appreciated to resolve this

Thanks,

Ahmed
  • 14,503
  • 22
  • 92
  • 150

1 Answers1

2

MessageBox is ok to be called from a different thread only if you are not assigning the main form (which was created on the UI thread, and is therefore bound to it) as the IWin32Window that owns the Messagebox.

Use this overload instead:

result = MessageBox.Show(message, AppStrings.low_space_title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
Rotem
  • 21,452
  • 6
  • 62
  • 109