Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange error in MonthCalendar just started happening out of nowhere

Tags:

c#

winforms

This crash report started coming through 2 days ago from a lot of our users. There have been no updates to our code, and this error is happening on multiple versions. Some of these versions are years old so it's not like an update broke something. It's a .NET 4.0 app using Infragistics 2014 Vol 2. Infragistics support seem to think it's a bug in the MonthCalendar for Windows Forms but I haven't been able to find anything online. From the screenshot with the crash dump it looks like all the users are doing is clicking to dropdown the calendar, when they get this error. I haven't been able to replicate it.

System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Windows.Forms.MonthCalendar.WndProc(Message& m)
at Infragistics.Win.MonthDropDownWithUIPermissions.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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Does anyone have any ideas what could be causing this? It's critical, and I have nowhere to even start.

like image 529
Jon Avatar asked May 15 '15 14:05

Jon


3 Answers

Thanks for Mangist response. It help me find the cause of the problem. I was having problems with the MonthCalendar control, the source of the problem was control's internal methods for displaying bolded dates. I was filling the BoldedDates property with an array of Dates. That was causing the control to automatically call the UpdateBoldedDates() function and there was the crash. All I did was that I replaced the code by adding bolded date one by one (there is no performance loss). When doing this, the UpdateBoldedDates() function must be called manually ( which saved my life :) ). So, after filling up the MonthCalendar with bolded dates I used the "workaround" and called the UpdateBoldedDates() function in the worker thread. Here is my code (in Visual Basic.NET):

Private Sub UpdateBoldedDatesWT()
    mcCalendar.UpdateBoldedDates()
End Sub

Public Sub LoadBoldedDates()

    Dim bDates As List(Of Date)

    Try
        Dim dExt As New DatesHelper(sqlConn)
        bDates = dExt.GetAppointmentDates(mcCalendar.SelectionStart)
        mcCalendar.RemoveAllBoldedDates()

        For Each d As Date In bDates
            mcCalendar.AddBoldedDate(d)
        Next

        mcCalendar.BeginInvoke(New MethodInvoker(AddressOf UpdateBoldedDatesWT))
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error loading bolded dates")
    End Try

End Sub
like image 169
Marjan Slavkovski Avatar answered Nov 19 '22 19:11

Marjan Slavkovski


Here is a Microsoft Hotfix rollup 3064711 for the Crash:

https://support.microsoft.com/en-us/kb/3064711

Problems solved:

Assume that you are running a Windows Forms application. When the application disposes a System.Windows.Forms.MonthCalendar control in certain cases, the following InvalidOperationException exception will be thrown: Operation is not valid due to the current state of the object.

MS Connect Thread: https://connect.microsoft.com/VisualStudio/feedback/details/1345128/errors-after-installing-kb3023222-update

like image 39
d1no Avatar answered Nov 19 '22 18:11

d1no


After I installed the Windows updates from Tuesday and rebooted my PC, I could replicate the problem easily. It turns out this (6 year old!) code now crashes .NET even though it's been working for years. I have code in one of my forms to close up the calendar after the user chooses a date. Now that line of code is crashing, so I have removed it. It looks like the calendar automatically closes up now without needing that anymore.

private void dtpServiceDate_ValueChanged(object sender, EventArgs e)
{
    dtpServiceDate.CloseUp(); // <-- this line crashes now

Commenting out that line fixes it.

I wish Microsoft wouldn't release breaking changes to the framework like this, now I have to update thousands of users apps that were working just fine.

EDIT: I have received an answer from Infragistics at least for a workaround. They are hesitant to change their code as it hasn't changed since 2002.

private void dtpServiceDate_ValueChanged(object sender, EventArgs e)
        {
            this.dtpServiceDate.BeginInvoke(new MethodInvoker(this.CloseCalendar));
        }

       private void CloseCalendar()
      {
            dtpServiceDate.CloseUp();
      }
like image 42
Jon Avatar answered Nov 19 '22 19:11

Jon