- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码使用 System.Windows.Threading.DispatcherTimer
设置计时器。
有一天,我发现自己试图设置一个比平常更大的计时器,但我得到了一个ArgumentOutOfRangeException
:
System.ArgumentOutOfRangeException: TimeSpan period must be less than or equal to Int32.MaxValue.
但是,即使在 .NET TimeSpan
使用的 100ns 单位(“滴答”)中,我的值(大约 41 天 & 15½ 小时,或 3,598,196,944 毫秒,或 3.5981969e+13 个滴答 —不可否认,很多滴答声)应该在 64 位整数的范围内,这就是文档声称的 TimeSpan
在内部使用(并且由 TimeSpan
构造函数使用)。
因为 Int32.MaxValue
是 2,147,483,647,所以建议 DispatcherTimer
仅支持长达近 25 天的持续时间。但是 DispatcherTimer
文档没有提到这个限制。
我进行了一些测试,确实:
#include <cstdint>
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Threading;
void tick(System::Object^ sender, System::EventArgs^ args) {}
int main()
{
// Duration is in 100ns units (10m per second).
const TimeSpan delay1(2147483647ULL * 10000);
const TimeSpan delay2(2147483648ULL * 10000);
// Fine
DispatcherTimer^ timer1 = gcnew DispatcherTimer(
delay1,
DispatcherPriority::Normal,
gcnew EventHandler(&tick),
Dispatcher::CurrentDispatcher
);
// System.ArgumentOutOfRangeException
DispatcherTimer^ timer2 = gcnew DispatcherTimer(
delay2,
DispatcherPriority::Normal,
gcnew EventHandler(&tick),
Dispatcher::CurrentDispatcher
);
}
我的意思是,好吧,我可以通过按顺序设置较短的延迟(比如一次最多一天)来解决这个问题,但我想尽可能避免这种复杂性。
我的观察准确吗?如果是这样,是否有任何方法可以改变这种未记录的行为?
[Windows 10 (v1803)、Visual Studio 2019 (v16.4.5)、.NET 4.7.03056、x64 build]
最佳答案
当文档失败时,让我们看看旧的源代码:
if (interval.TotalMilliseconds > Int32.MaxValue)
throw new ArgumentOutOfRangeException("interval", SR.Get(SRID.TimeSpanPeriodOutOfRange_TooLarge));
在我看来,您的观察非常准确。如果不向 Microsoft 提交票证,看起来也没有什么好方法可以改变这种未记录的行为。
他们为什么要在其中设置看似人为的限制?这条线看起来足够揭示:
_dueTimeInTicks = Environment.TickCount + (int)_interval.TotalMilliseconds;
当您只需验证输入并关闭工单时,为什么要重构代码来解决极端情况? :)
关于.net - 如何让 DispatcherTimer 设置一个超过 24 天的计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60367060/
问题一:您好,我想知道有没有一种方法可以处置或终止 DispatcherTimer 的对象并创建同名的新对象? 问题二:如果它设置为 Public,我可以访问其他类中的 DispatcherTimer
我很好奇为什么调度程序计时器在控制台模式下不起作用。我创建了一个简单的警报,当计时器达到限制时它会执行一些操作。 您可以在 UnitTest 或控制台模式下使用调度程序计时器吗?当我在表单中运行 Da
我想每隔几秒加载一张图片。我创建了 DispatcherTimer,我希望在计时器滴答中什么都不做,只是等待间隔。我该怎么做? if (window.DialogResult == true) {
我有一个应用程序的行为类似于自动刷新。它检查电子邮件、新视频、笑话等。 我的客户希望以不同的时间间隔检查这些单个元素。例如,每分钟发送一次电子邮件,每小时发送一次视频,等等。因此,他应该可以选择自己将
为什么在某些类中我必须指定此构造函数才能调用回调? _timer = new DispatcherTimer(DispatcherPriority.Normal, Application.Curren
我仍在学习 C#,我需要一些帮助。我有一个调度计时器,它每秒检查一次日期时间是否已初始化。它有效,但问题是它会继续做同样的事情,直到日期时间结束。例如,我希望 datetime 启动记事本。问题是 d
我正尝试在我的应用程序中使用三个按钮 - 开始、停止和重新启动来实现计时器。 计时器可以正常启动和停止。但是,当我单击重新启动按钮时,它从先前的秒和分钟值开始,而不是从 0 和 0 开始。我看不出我在
我正在尝试使用调度计时器,但我的 C# 应用找不到命名空间。这是错误: The type or namespace name 'DispatcherTimer' could not be found
我刚刚开始开发 Windows 应用商店应用程序,我只想要一个非常简单的应用程序:几乎是一个从左到右填满的进度条,但即使是这个任务对我来说显然也无法完成。 我有以下代码: using System;
如何重置 dispatcherTimer? 在我的应用程序中,我启动了一个计时器,稍后我使用 dispatcherTimer.Stop 方法停止,计时器停止。我关闭应用程序并再次运行它,但计时器并未从
我正在使用 DispatcherTimer 在 C# 中调用 void: counter = new System.Windows.Threading.DispatcherTimer(
我正在使用调度程序源计时器以不同的帧速率更新 View 。 (8、12 或 24 FPS) 这里是初始化dispatcherTimer的代码和用于创建计时器的函数。 (此函数直接取自苹果文档中的“创建
我需要用实时摄像机每秒捕捉 10 帧,持续 15 分钟。我使用了 dispatcherTimer 类,但它似乎不能正常工作。如何做到准确?我什至不保存图像,因为它很耗时,我只是将它保存在内存中,但它仍
我正在编写一个 Windows Phone 8.1 应用程序 (WINRT)。 我正在显示一个StatusBar: MYAPP(欢迎) MYAPP(通过 WIFI 连接) MYAPP(通过手机连接)
基本上,当我们应用某个时间间隔(即 5 秒)时,我们必须等待它。 是否可以应用间隔并立即执行计时器而不等待 5 秒?(我指的是间隔时间)。 有什么线索吗? 谢谢!! public partial cl
这些是我的 DispatcherTimer 声明和方法: private DispatcherTimer DishTimer; private TimeSpan SpanTime; private v
好的!这听起来像是“让我为你谷歌那个”,好吧,我做到了,这里是 MSDN 链接: http://msdn.microsoft.com/en-us/library/system.windows.thre
System.Windows.Forms.Timer() 和 System.Windows.Threading.DispatcherTimer() 有什么区别?在哪些情况下,我们应该使用它们?有什么最
我在使用 WPF 的项目上遇到问题。我正在尝试使用 DispatchTimer。我在另一个例子中看到 using DispatcherTimer = Windows.UI.Xaml.Dispatche
我在 StackOverflow 上看到了有关如何正确删除 WPF 中的控件的问题。通常有一些评论如何不手动(或不能)处理它们,只要您不持有对它们的引用,它们最终将被 GC 清除。我很偶然地注意到,我
我是一名优秀的程序员,十分优秀!