C# DateTimePicker 陷入循环
我有一个 C# 中的日期时间选择器。当我单击它时,它会展开以显示月历,当我单击向左箭头返回一个月时,它会更改值并调用我的事件。该事件包含的代码太多,无法在此包含,但它调用了几个函数,不用说。
我遇到的问题是,当我单击左箭头时,它会陷入某种循环,并在几个月内不断下降,我无法阻止它。正在调用的函数之一包含 Application.DoEvents() ,如果我注释掉它,它不会陷入循环,但我需要该命令来更新界面的另一部分。知道为什么会发生这种情况吗?
有时我可以用这段代码复制它,有时它只执行几次,有时它会卡在循环中。
private void DateTimePickerValueChangedEvent(object sender, EventArgs e)
{
afunction();
}
private void afunction()
{
listView1.Clear();
panel1.Visible = true;
Application.DoEvents();
}
I have a datetimepicker in C#. When I click on it, it expands to show a monthly calendar, when I click the left arrow to go back a month, it changes the value and calls my event. The event includes too much code to include here but it calls several functions needless to say.
The problem I'm having is that when I click that left arrow it gets stuck in some sort of loop and keeps descending through the months and I can't stop it. One of the functions that is being called contains a Application.DoEvents() and if I comment that out it doesn't get stuck in the loop, but I need that 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 times, sometimes it gets stuck in the loop.
private void DateTimePickerValueChangedEvent(object sender, EventArgs e)
{
afunction();
}
private void afunction()
{
listView1.Clear();
panel1.Visible = true;
Application.DoEvents();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我也有同样的问题。就我而言,我不是调用 DoEvents,而是更新 Crystal Report 视图。我发现的唯一解决方法是在 CloseUp 事件而不是 ValueChanged 或 TextChanged 时更新我的视图。
斯科特,你最后是如何解决你的问题的?
I also have the same problem. In my case, instead of calling DoEvents I'm updating a Crystal Report view. The only workaround I found is to update my view upon the CloseUp event instead of ValueChanged or TextChanged.
Scott, how did you finally corrected your problem ?
DateTimePicker ValueChanged 事件有错误。根据此页面上的 Microsoft Windows Forms 团队 https: //connect.microsoft.com/VisualStudio/feedback/details/1290685/debugging-datetimepicker-event-hangs-vs:
“DateTimePicker 控件安装鼠标挂钩作为其功能的一部分,但是当调试器具有WinForms 应用程序在断点处停止,如果 VS 碰巧收到鼠标消息,则可能会出现死锁,不幸的是,死锁是 DateTimePicker 设计的结果,当单击下拉菜单时安装了鼠标钩子。这意味着不应在日历处于活动状态时调用的任何事件处理程序中发送断点。我们目前正在研究是否可以解决此问题,如果可以的话,我们将用更多信息更新此线程。以便进行修复。”
The DateTimePicker ValueChanged event is buggy. Per Microsoft Windows Forms Team on this page https://connect.microsoft.com/VisualStudio/feedback/details/1290685/debugging-datetimepicker-event-hangs-vs:
"The DateTimePicker control installs a mouse hook as part of its functionality, but when the debugger has the WinForms application stopped on a breakpoint, it allows the possibility of a deadlock if VS happens to get a mouse message. For now, the deadlock is unfortunately a consequence of the DateTimePicker's design. The mouse hook is installed when the drop down is clicked to display the calendar. This means that breakpoints should not be sent in any event handlers which would be called while the calendar is active. We are currently investigating whether it is possible to address this issue and we will update this thread with further information if we are able to make a fix available."
在看不到任何代码的情况下,请尝试以下步骤:
看看它什么都没有跑得有多快
附于其上。
哪些造成的影响最大
问题。
Without seeing any of the code, try these steps:
to see how fast it runs with nothing
attached to it.
which ones are causing the most
problems.
你可以尝试几件事。删除 ChangedEvent 中的 DoEvents。
可能在一段时间后调用单独函数内的 doevents (thread.sleep() ?)。
我知道 doevents 确实会引起问题,但我很少使用它。
You could try a couple of things. Get rid of the DoEvents inside of the ChangedEvent.
Call the doevents inside of a seperate function after maybe a period of time (thread.sleep() ?).
I know doevents does cause issues but I rarely use it.
事件过程
ValueChanged
:在
sender.tag
中设置参数使用 sender.tag 启用定时器并执行参数
示例:
event procedure
ValueChanged
:set parameter in
sender.tag
enableTimer and execute parameter using sender.tag
example: