InvalidCastException when editing a cell
ObjectListView - ListView on caffeine, guarana and steroids
Brought to you by:
grammarian
I have an OLV and I create its OLVColumns at runtime. The first has checkboxes, while the second is the only one that is editable.
I'm experiencing this issue: I edit a cell, when I terminate I get InvalidCastException (not handled) and from now on I get the same exception every time I start or end editing a cell.
Exception details:
System.InvalidCastException: Cast specificato non valido.
in System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
in System.Windows.Forms.StringSource..ctor(String[] strings)
in System.Windows.Forms.TextBox.SetAutoComplete(Boolean reset)
in System.Windows.Forms.TextBox.OnHandleCreated(EventArgs e)
in System.Windows.Forms.Control.WmCreate(Message& m)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.TextBoxBase.WndProc(Message& m)
in System.Windows.Forms.TextBox.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I can post the list of loaded assemblies if needed.
Thanks
Another screenshot, with the error.
I haven't seen this error before.
After looking around a bit, I found this thread which (towards the end) suggests that the CoCreateInstance fails because of some sort of threading problem.
I've looked at the code in OLV that handles the auto completion, and there doesn't seem to be any multi-threading involved.
Do you do anything that might be using multi-threading?
Does the problem happen reliably?
Can you post a full sample code that shows the problem?
Thank you indeed Phillip.
I read the discussion you posted and I found also these ones:
I'm in a complex environment: a big application launches my program as an add-on. Posting the full code would be difficult.
I played with threads, setting or not setting
[STAThread]on the main and also putting the form with the OLV on a new thread (trying both with and withoutthread.SetApartmentState(ApartmentState.STA)). But the exception occures again.A strange thing is that when debugging there's no exception, while it happens every time in production.
While looking for a solution, I disable autocomplete on the OLV column, ad I don't need it:
I'm glad you found a work around. I have no idea how to actually fix the issue.
From the looks of all the other messages, it seems that this issue actually has nothing to do with OLV.
Hello Phillip!
I have a similar problem. Also getting a InvalidCastException when clicking an editable column. The exception happens at this part of code in StartCellEdit:
If I pause at the line with the debugger and then continue, the code seems mysteriously to work. So it must have something to do with multithreading. I have checked in the debugger that the control is correctly created (just a simple TextBox).
As **work-around ** I disabled auto-completion on that column and everything works fine! So, I assume that the auto-completion feature is somehow involved.
Perhaps this description gives you a hint how to debug the problem. I am working in a tiny demo project, which I could send you to reproduce the problem.