views:

113

answers:

4

Working on a .NET app, I've run in a 'cross-thread operation not valid' exception, only it seems to happen in the correct thread. Is there a way to find out which thread is the one where a specific control has been created?

What I've found so far:

The 'InvokeRequired' operation only tells IF the current thread is the "owner thread"... A bit of fun time with Reflector on the Control.Invoke(...) method got me to a P/Invoke method in user32.dll that gets the thread Id from a window handle:

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern int GetWindowThreadProcessId(HandleRef hWnd, out int lpdwProcessId);
A: 

Can you capture the thread ID when the event is created in a handler and compare that to when you are getting the exception. That will at least tell you if you are on the same thread.

rerun
By 'event', you mean 'control'? The strange thing is, both the thread where the control gets created and the thread where the exception happens are one and the same, namely the main thread of the application. I'm thinking the {control -> owner thread} mapping gets somehow screwed up.
Cristi Diaconescu
A: 

Control.BeginInvoke() is always worked fine for me. Try it.

Vasiliy Borovyak
A: 

Try using Spy++ application, it comes with VS and shows you a list of windows (most controls are separate subwindows) and some information about them.

Pasi Savolainen
+1  A: 

I've encountered the same issue. I get that exception even when using the control from the UI thread. In my case, I was using InvokeRequired (or Invoke) on a background thread before the Handle of the control was created. It was a context menu of a tray icon, and some background thread had to change the value of a menu item. If the user never opened the context menu, the handle was never created, the control was never bound to the UI thread, and havoc ensued. When this happened, the InvokeRequired always returned false and Invoke just ran the method on the current thread (which was not the UI thread), and so the Handle was created on the background thread and the control was forever bound to that thread, as if the background thread was its UI thread. And when trying to use the control form the UI thread, a cross-thread exception was thrown. On the other hand, if the user opened the context menu before running any background threads, everything would work fine.

The solution was to call the CreateControl() method from the UI thread at startup, before any background thread has chance to 'steal' the control and to corrupt thread ownership.

Allon Guralnek
Great explanation. Thanks!
Cristi Diaconescu
Did that actually solve your problem? BTW, I have just found out that the same explanation exists in the MSDN article of the `Control.InvokeRequired` method, under the Remarks section. I could swear that it wasn't there a few years ago (when that behavior drove me mad). http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx
Allon Guralnek