Menu

Can not access disposed object in CellEditFinished event popup messages of ObjectListView

Kevin Yang
2017-06-27
2017-06-27
  • Kevin Yang

    Kevin Yang - 2017-06-27

    When I click on the 'Open Modal Window' button in [Main form], it shows the [subform] that is built with a data list by using the ObjectListView component.

    When I click on the "Age" column to edit it and changed the value in the cell to be a non zero value, then click on the blank of the ObjectListView component(if I press Enter, the exception will not come out), it popup a message. I left this message in front of all the opened windows and wait 10 seconds for the application timeout. At that time, an exception shows:

    Cannot access a disposed object. Object name: 'ObjectListView'.
    Here is the code:

    private void button1_Click(object sender, EventArgs e)
    {
    modalForm = new Form2();
    StartTimeOutTimer();
    var result = modalForm.ShowDialog(this);
    }

    private void ProcessTimeOut()
    {
        timer1.Stop();
        seconds = 0;
        List<Form> openForms =
                Application.OpenForms.Cast<Form>().Where(frm => !string.Equals(frm.Name, "Form1") && !frm.IsDisposed).ToList();
        for (var i = openForms.Count - 1; i >= 0; i--)
        {
            openForms[i].Dispose();
        }
    }
    

    the exception come out in this line:

    var result = modalForm.ShowDialog(this);
    The sample code and solution is here:

    https://1drv.ms/u/s!AmQYEYkCV6gqgw6iPMVFPBKlSx1V

    I tried to add the source code of ObjectListView in my solution, and the error occurred in the GetHeaderControl method in the source code of v2.9.0

    Please download my sample code to reproduce the exception. How do I fix this exception? Any suggestion will be appreciated.

     

    Last edit: Kevin Yang 2017-06-27
  • Phillip Piper

    Phillip Piper - 2017-06-27

    This seems to have nothing to do with ObjectListView.

    You can't dispose of WinForms like that. You have to close the modalForm from within.

    Put the timer within the modalForm and when it fires, call Close().

     
    • Kevin Yang

      Kevin Yang - 2017-06-29

      But it seems that the "LeftButtonClick" event has been fired after the "CellEditFinished" event when I see the StackTrace below:
      ~~~
      at System.Windows.Forms.Control.CreateHandle()
      at System.Windows.Forms.ListView.CreateHandle()
      at System.Windows.Forms.Control.get_Handle()
      at BrightIdeasSoftware.NativeMethods.GetHeaderControl(ListView list)
      at BrightIdeasSoftware.HeaderControl..ctor(ObjectListView olv)
      at BrightIdeasSoftware.ObjectListView.get_HeaderControl()
      at BrightIdeasSoftware.ObjectListView.LowLevelHitTest(Int32 x, Int32 y)
      at BrightIdeasSoftware.ObjectListView.OlvHitTest(Int32 x, Int32 y)
      *** at BrightIdeasSoftware.ObjectListView.HandleLButtonDown(Message& m)***
      at BrightIdeasSoftware.ObjectListView.WndProc(Message& m)
      at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
      at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
      at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
      at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
      at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
      at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
      at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
      at System.Windows.Forms.Application.RunDialog(Form form)
      at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
      at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in e:\PracticeSpace\OLVTest\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 26
      at System.Windows.Forms.Control.OnClick(EventArgs e)
      at System.Windows.Forms.Button.OnClick(EventArgs e)
      at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
      at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
      at System.Windows.Forms.Control.WndProc(Message& m)
      at System.Windows.Forms.ButtonBase.WndProc(Message& m)
      at System.Windows.Forms.Button.WndProc(Message& m)
      at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
      at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
      at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
      at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
      at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
      at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
      at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
      at System.Windows.Forms.Application.Run(Form mainForm)
      at WindowsFormsApplication1.Program.Main() in e:\PracticeSpace\OLVTest\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
      at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
      at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
      at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
      at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
      at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
      at System.Threading.ThreadHelper.ThreadStart()
      ~~~
      especially at the "at BrightIdeasSoftware.ObjectListView.HandleLButtonDown(Message& m)"line.
      I don't know why the "LeftButtonClick" event still fired, and how prevent it from firing.

       

      Last edit: Kevin Yang 2017-06-29
  • Phillip Piper

    Phillip Piper - 2017-07-04

    I have no idea about that stack trace.

    But whileever you are trying to Dispose of forms from a background thread, there are going to be problems.

     

Log in to post a comment.

MongoDB Logo MongoDB