2

Possible Duplicate:
Cross-thread operation not valid

I am trying to close the base of a form from another thread. I am getting the following error.

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

for the below line:

MDIParent.MDIParentRef.BaseClose();
Community
  • 1
  • 1
Anuya
  • 8,082
  • 49
  • 137
  • 222

1 Answers1

4

You need to perform the operation on the UI Thread:

if (InvokeRequired) 
    Invoke(new Action(MDIParent.MDIParentRef.BaseClose));
else
    MDIParent.MDIParentRef.BaseClose();
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • @Mehrdad, i tried to use as above. am getting error : Cannot convert lambada expression to type "system.delegate" because it is not a delegate type. – Anuya Sep 09 '09 at 03:22
  • @Mehrdad, if(InvokeRequired) always have the value as false. Because of that the else condition is executing and same error as above is coming again. – Anuya Sep 09 '09 at 03:26
  • karthk: that's a little weird. If you're sure you're calling it from another thread, replace the whole thing with the `Invoke(...)` line. – Mehrdad Afshari Sep 09 '09 at 03:27
  • @Mehrdad, the reason why i am using BaseClose() is.. I am using shellLib and creating a custom desktop task bar. When i try to close the task bar using BaseClose(), it closes that perfectly. But the problem is the space occupied by taskbar is blocked and it is not released. what would be the problem. Advise me on this. Thanks. – Anuya Sep 09 '09 at 03:31
  • karthik: That's a separate issue. I'm not sure what you should do. Post it as a separate question. Someone will answer. – Mehrdad Afshari Sep 09 '09 at 03:33