- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到过解释“要取消挂起的 OnTime 事件,您必须提供它计划运行的确切时间”的帖子。
我应该提供事件第一次运行的时间,还是应该提供下一次触发事件的时间?
我已经尝试了两个版本的 StopTimer。两个都给我
Method OnTime of object _Application failed
Option Explicit
Private Sub Workbook_Open()
count = 1
Call test
End Sub
Public runwhen As Double
Public Const runwhat As String = "TheSub"
Public firstrunTime As Double
Public count As Integer
Sub test()
If count = 1 Then
runwhen = Now + TimeSerial(0, 0, 5)
firstrunTime = runwhen
Else
runwhen = Now + TimeSerial(0, 0, 5)
End If
Application.OnTime runwhen, "TheSub"
End Sub
Sub TheSub()
MsgBox "Hi!!!!!!"
count = count + 1
Call test
If count = 5 Then
StopTimer
End If
End Sub
'First Version of StopTimer
Sub StopTimer()
Application.OnTime firstrunTime, "TheSub", , False
End Sub
'Second Version of StopTimer
Sub StopTimer()
runwhen=now+TimeSerial(0,0,5)
Application.OnTime runwhen, "TheSub", , False
End Sub
Sub test()
If count = 1 Then
runwhen = Now + TimeSerial(0, 0, 5)
firstrunTime = runwhen
Else
runwhen = Now + TimeSerial(0, 0, 5)
End If
If count <> 5 Then
Application.OnTime runwhen, "TheSub"
Else
Call StopTimer
End If
End Sub
最佳答案
要取消 OnTime 事件,您需要告知计划运行的时间。
您的第一次尝试是告诉它取消不再安排的预定事件 - 它实际上可能在几个小时前发生。
您的第二次尝试是告诉它取消计划的事件,该事件将在您决定要取消事件后 5 秒发生。你 可能幸运的是,在您设置后很快就决定取消它,5 秒是正确的时间,但您可能不会。 (这取决于时钟的准确度以及计算机执行代码的速度。)
你需要做的是告诉它在你设置它的同时取消事件,所以你的代码需要说:
'Third Version of StopTimer
Sub StopTimer()
Application.OnTime runwhen, "TheSub", , False
End Sub
runwhen
)。
runwhen
当你不应该的时候。
06:00:00.000 - Workbook opens
06:00:00.001 - Subroutine Test is called
06:00:00.002 - Count is 1, so first If statement executes the first section
06:00:00.003 - runwhen is set to 06:00:05.003
06:00:00.004 - firstruntime is set to 06:00:05.003
06:00:00.005 - Count is 1, not 5, so second If statement executes the first section
06:00:00.006 - OnTime is set to run TheSub at 06:00:05.003
06:00:00.007 - Subroutine Test finishes and control returns to TheSub
06:00:00.008 - Count is 1, not 5, so If statement is not executed
06:00:00.009 - Subroutine TheSub finishes and execution of macro stops
06:00:05.003 - OnTime event triggers
06:00:05.004 - Subroutine TheSub is called
06:00:05.005 - MsgBox is displayed
The user is very slow to press the button this time. (Mainly because I had
written a lot of the following times, and then realised my Count was out
by 1, and I didn't want to have to rewrite everything - so I just added
a very slow response here.)
06:00:12.000 - User presses OK
06:00:12.001 - Count is set to 2
06:00:12.002 - Subroutine Test is called
06:00:12.003 - Count is 2, not 1, so first If statement falls into Else portion
06:00:12.004 - runwhen is set to 06:00:17.004
06:00:12.005 - Count is 2, not 5, so second If statement executes the first section
06:00:12.006 - OnTime is set to run TheSub at 06:00:17.004
06:00:12.007 - Subroutine Test finishes and control returns to TheSub
06:00:12.008 - Count is 2, not 5, so If statement is not executed
06:00:12.009 - Subroutine TheSub finishes and execution of macro stops
06:00:17.004 - OnTime event triggers
06:00:17.005 - Subroutine TheSub is called
06:00:17.006 - MsgBox is displayed
06:00:18.000 - User presses OK
06:00:18.001 - Count is set to 3
06:00:18.002 - Subroutine Test is called
06:00:18.003 - Count is 3, not 1, so first If statement falls into Else portion
06:00:18.004 - runwhen is set to 06:00:23.004
06:00:18.005 - Count is 3, not 5, so second If statement executes the first section
06:00:18.006 - OnTime is set to run TheSub at 06:00:23.004
06:00:18.007 - Subroutine Test finishes and control returns to TheSub
06:00:18.008 - Count is 3, not 5, so If statement is not executed
06:00:18.009 - Subroutine TheSub finishes and execution of macro stops
06:00:23.004 - OnTime event triggers
06:00:23.005 - Subroutine TheSub is called
06:00:23.006 - MsgBox is displayed
06:00:24.000 - User presses OK
06:00:24.001 - Count is set to 4
06:00:24.002 - Subroutine Test is called
06:00:24.003 - Count is 4, not 1, so first If statement falls into Else portion
06:00:24.004 - runwhen is set to 06:00:29.004
06:00:24.005 - Count is 4, not 5, so second If statement executes the first section
06:00:24.006 - OnTime is set to run TheSub at 06:00:29.004
06:00:24.007 - Subroutine Test finishes and control returns to TheSub
06:00:24.008 - Count is 4, not 5, so If statement is not executed
06:00:24.009 - Subroutine TheSub finishes and execution of macro stops
06:00:29.004 - OnTime event triggers
06:00:29.005 - Subroutine TheSub is called
06:00:29.006 - MsgBox is displayed
06:00:30.000 - User presses OK
06:00:30.001 - Count is set to 5
06:00:30.002 - Subroutine Test is called
06:00:30.003 - Count is 5, not 1, so first If statement falls into Else portion
06:00:30.004 - runwhen is set to 06:00:35.004
06:00:30.005 - Count is 5, so second If statement executes falls into the Else portion
06:00:30.006 - Subroutine StopTimer is called
06:00:30.007 - Code attempts to cancel Ontime event scheduled for 06:00:35.004 (the value of runwhen),
but fails because no such event is scheduled)
runwhen
的值(在我的示例中为 06:00:30.004),但不要设置 OnTime 事件。然后您去取消该事件,但它不能取消。
runwhen
仅限 当您设置 OnTime 事件时,您将能够使用该变量来取消事件。
'In your Workbook module
Option Explicit
Private Sub Workbook_Open()
count = 1
Call StartTimer
End Sub
'In your main code module
Option Explicit
Public runwhen As Double
Public count As Integer
Sub TheSub()
MsgBox "Hi!!!!!!"
count = count + 1
Call StartTimer
End Sub
Sub StartTimer()
If count <> 5 Then
runwhen = Now + TimeSerial(0, 0, 5)
Application.OnTime runwhen, "TheSub"
End If
End Sub
Application.OnTime runwhen, "TheSub", , False
关于excel - 停止 OnTime 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39057696/
我写下了下面的VBA代码,每5秒执行一次,但它不起作用。 Option Explicit Public RunWhen As Double Public Const cRunIntervalSecon
我一直在尝试使用 Apache Beam 的计时器,但无法触发它们。 据我所知,您可以在 DoFn 中按以下方式定义计时器。 @TimerId("expiry") private final Time
我最近一直在处理一个让我发疯的问题,因为它只是在部署到 Dataflow 中后才会发生,但从来没有在本地运行过,一切都完美无缺。仅供引用,我正在使用 Apache Beam 2.9.0。 我正在定义一
我有一个分配给 StopRecordingData 子的按钮来取消两个子,但它没有。时间表 False 似乎并没有取消 que 中的预定潜艇。 Dim NextTime As Double Sub R
我遇到过解释“要取消挂起的 OnTime 事件,您必须提供它计划运行的确切时间”的帖子。 我应该提供事件第一次运行的时间,还是应该提供下一次触发事件的时间? 我已经尝试了两个版本的 StopTimer
我正在尝试取消 ThisWorkbook 模块的 Workbook_Close 例程中的计时器。谁能解释以下行为?: 手动关闭工作簿 Application.OnTime 按预期运行并取消计时器。 如
我正在使用 Application.Ontime 命令在一段时间不活动(10 分钟)后自动关闭电子表格。 以下代码似乎可以正常工作,但是,如果您自己手动关闭工作表,工作簿似乎仍然在后台处于事件状态,并
基本上我有一个程序,它有 6 个通过串行端口发送值的 slider ,我使用计时器在用户更改这些值时动态重新发送这些值。我遇到的问题是我的计时器调用的 6 个函数的行为很奇怪,第一个函数调用正常工作,
我的应用程序中有一个 TTimer,每 2 秒触发一次并调用我的事件处理程序 HandleTimerEvent()。 HandleTimerEvent() 函数会修改共享资源,并且在返回之前可能需要
我需要每天在特定时间运行我的 Excel VBA。我用谷歌搜索了 ontime 方法,但是我没有从中得到全面的理解。我会提出这个来消除我对 ontime 方法的困惑。 在运行之前,包含的 Excel
我正在开发 Excel 2010 的数据采集前端。我无法弄清楚将多个局部变量参数传递给 Application.OnTime 的语法。 http://markrowlinson.co.uk/artic
vba 函数中字符串中可以使用的最大字符数为 255。我正在尝试运行这个函数 Var1= 1 Var2= 2 . . . Var256 =256 RunMacros= "'Tims_pet_Robot
我已经阅读了一些关于如何停止 VBA Application.OnTime 的问题/线程(例如 this 和 this )程序,但我就是无法停止! 我用它每 x 秒提取一些数据。我明白当我调用 OnT
这里遇到了一些麻烦,现在我只是想让一个图像在屏幕上移动,变量和定位是有效的,但由于某种原因,OnTimer 从未被调用,我在其中放置了一个断点,但它从未被调用到达。没有错误,所以我假设所有方法和变量都
我有一个使用 ON_WM_TIMER 的 MFC 应用程序。 前 1-2 分钟,我用 启动计时器 m_nIDEvent = SetTimer( 234, 500, NULL ); 这工作得很好,但在运
我不确定我是否在做一些没有记录的事情。我创建了自己的派生自 CListCtrl 的类然后覆盖其中的 OnTimer 处理程序: void CListCtrl2::OnTimer(UINT_PTR nI
通常,当人们收到“'onTimer' 没有重载匹配委托(delegate) 'ElapsedEventHandler'”错误时,这是由于没有在每次计时器结束时运行的 void 中包含 Elapse
我使用 application.ontime 在 Excel 中的 VBA 中编写了一个计时器当到达结束时间时,它会发出声音并给出一个消息框。 当时间在同一天晚些时候时,这很好用,但是当它到第二天时,
我正在尝试使用类似于此的代码在宏中实现递归功能-: Dim showTime As Boolean Sub RunClock() Range("A1", "A1").Valu
我想制作一个在后台循环运行的程序,并在可刷新的查询上运行,而不会同时挂起 excel,当发生错误时,它会显示一条消息。 因此,对我有用的唯一想法是使用 Application.Ontime 安排一个过
我是一名优秀的程序员,十分优秀!