C # DateTimePicker stuck in a loop

I have a datetimepicker in C #. When I click on it it expands to show the monthly calendar, when I click the left arrow to go back to the month it changes the value and triggers my event. The event includes too much code to include here, but it calls several functions, not to mention the need. The problem I'm running into is that when I click on this arrow on the left it gets stuck in some kind of loop and keeps going down months later and I can't stop it. One of the called functions contains Application.DoEvents () and if I comment that it doesn't get stuck in a loop, but I need this command to update another section of the interface. Any idea why this is happening?

I can duplicate it sometimes with this code, sometimes it just does it a couple of times, sometimes it gets stuck in a loop.

private void DateTimePickerValueChangedEvent(object sender, EventArgs e) 
{ 
afunction(); 
} 

private void afunction() 
{ 
listView1.Clear(); 
panel1.Visible = true; 
Application.DoEvents(); 
} 

      

+2


source to share


4 answers


I have the same problem too. In my case, instead of calling DoEvents, I am updating the Crystal Report view. The only workaround I have found is to update my view on CloseUp event instead of ValueChanged or TextChanged.



Scott, how did you finally fix your problem?
+4


source


Without looking at any code, follow these steps:



  • Comment out the entire event handler to see how fast it runs with nothing attached to it.
  • Uncomment lines one at a time to see which ones are the problem.
  • Analyze these method calls.
  • ...
  • Profit!
+1


source


DateTimePicker ValueChanged event is an error. On the Microsoft Windows Forms Team, this page https://connect.microsoft.com/VisualStudio/feedback/details/1290685/debugging-datetimepicker-event-hangs-vs :

"The DateTimePicker control sets the mouse hook as part of its functionality, but when the debugger has the WinForms application stopped at a breakpoint, it allows a deadlock if the VS Server receives a mouse message. Deadlock is unfortunately a consequence of the DateTimePicker design. The mouse hook is set. when clicking on the dropdown to display the calendar. This means breakpoints should not be sent in event handlers that will be called while the calendar is active. currently investigating if this can be resolved and we will update this thread with more information if we can make the fix available. "

+1


source


You can try a couple of things. Get rid of DoEvents inside ChangedEvent. Call events inside a separate function after perhaps a period of time (thread.sleep ()?).

I know the events caused problems, but I rarely use them.

0


source







All Articles