C# .NET - Different output in debugging and running mode

Asked By Abhinav Singh on 09-Jan-14 04:18 AM
Hi Guys,
I am working on a C# windows application. I am facing a wierd problem in my code. I am using a dropdown to get some date. There are two events to change the value, one for ValueSelected and another for ValueChanged. Whenever I run that code while debugging, it show the correct date but when I run that code w/o debugging, it always shows wrong date. I also print the StackTrace in both mode. I found that codes call different methods in different mode. Here is the Code and Stack Trace:
Code :
void cboPeriod_ValueSelected(object sender, PDIMultiColValueSelectEventArgs e)
{
    DataRow dr = cboPeriod.LookupGrid.CurrentRow;
    m_ThruDate = Convert.ToDateTime(libDataSetUtil.GetColValue(dr,       
           TC_Calendar_Detail.F_ENDINGDATE, m_NextPeriodBegDate.AddDays(-1) + M_ALL_DAY_LONG));
    m_FromDate = Convert.ToDateTime(libDataSetUtil.GetColValue(dr, TC_Calendar_Detail.F_BEGDATE, 
           m_CurPeriodBegDate));
            
    OnValueChanged();
}
 
void cboPeriod_ValueChanged(object sender, EventArgs e)
{
    DataRow dr = cboPeriod.LookupGrid.CurrentRow;
 
    m_ThruDate = Convert.ToDateTime(libDataSetUtil.GetColValue(dr, 
           TC_Calendar_Detail.F_ENDINGDATE, m_NextPeriodBegDate.AddDays(-1) + M_ALL_DAY_LONG));
    m_FromDate = Convert.ToDateTime(libDataSetUtil.GetColValue(dr, TC_Calendar_Detail.F_BEGDATE, 
           m_CurPeriodBegDate));
            
    OnValueChanged();
}
With Debugging :
   at profdata.com.GeneralLedger.frmGLRPTTrialBalance.datPeriodDate_ValueChanged(Object sender, EventArgs e)
   at profdata.com.Controls.PDIDatePicker.OnValueChanged() in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIDatePicker.cs:line 3804
 
  at profdata.com.Controls.PDIDatePicker.cboPeriod_ValueChanged(Object sender, EventArgs e) in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIDatePicker.cs:line 3018
   at profdata.com.Controls.PDIMultiCol.OnValueChanged() in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIMultiCol.cs:line 2415
   at profdata.com.Controls.PDIMultiCol.set_Value(String value) in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIMultiCol.cs:line 894
   at profdata.com.Controls.PDIMultiCol.SelectValue() in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIMultiCol.cs:line 1753
   
   at profdata.com.Controls.PDIMultiCol.m_Grid_Click(Object sender, EventArgs e) in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIMultiCol.cs:line 1351
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at profdata.com.Controls.PDIDataGrid.OnClick(EventArgs e) in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIDataGrid.cs:line 5108
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
Without Debugging :
   at profdata.com.GeneralLedger.frmGLRPTTrialBalance.datPeriodDate_ValueChanged(Object sender, EventArgs e)
   at profdata.com.Controls.PDIDatePicker.OnValueChanged() in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIDatePicker.cs:line 3804
   
   at profdata.com.Controls.PDIDatePicker.cboPeriod_ValueSelected(Object sender, PDIMultiColValueSelectEventArgs e) in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIDatePicker.cs:line 3007
   at profdata.com.Controls.PDIMultiCol.OnValueSelected(PDIMultiColValueSelectEventArgs e) in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIMultiCol.cs:line 1733
   at profdata.com.Controls.PDIMultiCol.SelectValue() in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIMultiCol.cs:line 1756
   
   at profdata.com.Controls.PDIMultiCol.m_Grid_Click(Object sender, EventArgs e) in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIMultiCol.cs:line 1351
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at profdata.com.Controls.PDIDataGrid.OnClick(EventArgs e) in D:\PDI\Sandboxes\Ent_6.8.RU.Dev\Enterprise - WS\Lib\PDICtlSet1\ctlPDIDataGrid.cs:line 5108
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
 
As you can see (the bold part in both stach trace), both stack traces are different. Without debugging, it is not calling the cboPeriod_ValueChanged event but calling cboPeriod_ValueSelected which should be called after ValueChanged or should not call. So do anyone have any idea that why this is happening. 
 
My Understanding: If I am giving this code a break (like hitting a breakpoint, putting message box or let the thread sleep for sometime, it works fine)
 
Sorry for this long question. :(
Robbe Morris replied to Abhinav Singh on 09-Jan-14 09:55 AM
Out of curiosity, why would you attempt to set the property value in both events?  You "appear" to execute the same code.  Won't each event fire and run duplicate code when the changed event fires?

Why not use just the changed event or perhaps just the value selected event.  I could see the value selected event if you were attempting to trigger/retrigger some UI functionality if the user selected item and wanted to fire logic even if they select the same item.

If you are just setting property values, you may have better luck retrieving the property values for the whole row when a button somewhere is clicked versus trying to constantly keep a class level object's properties current.