Issue with custom drawn TextBoxes and KeyDown/DoubleClick methods

167 views Asked by At

I am struggling with this exception under windows mobile 6.5

System.NotSupportedException was unhandled
Message="Control.Invoke must be used to interact with controls created on a separate thread."
StackTrace:
in Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
in System.Windows.Forms.Control.Dispose(Boolean disposing)
in OpenNETCF.Windows.Forms.OwnerDrawnList.Dispose()
in OpenNETCF.Windows.Forms.OwnerDrawnList.Finalize()

I am using a List of textboxes from OPENNETCF as a fancy looking menu. They are custom drawn, but I found out nothing is wrong with disposing / creating all those objects UNTIL a method in DoubleClick or KeyDown is invoked. In this method I initiate DataGrid and things that always worked.

Once I exit main form, this exception is thrown. It doesn't affect application in any way, except for that big ugly win32 exception at the end of using application. What am I missing? I am disposing those objects before redrawing a form.


@EDIT I managed to fix it by disposing all those objects in not Form.Disposed event which acts after parent class is disposed, but in Form.Closing event. My guess is NETCF uses some threading in the background, that is being disposed by windows.form.

1

There are 1 answers

0
BenCamps On

The reason why it is yelling at you for callinging a control method from another thread is because you allowing the Garbage Collector to clean up your controls, and while it is cleaning up the GC ends up calling Dispose for you. The reason why its says another thread is calling your controls is because the garbage collector runs in a separate thread. To fix your problem you can use a Using statement or directly call Dispose on your form when your program closes.