我目前在我的cpp(C ++ 11)程序中使用libevent-2.0.5来设置计时器事件。
代码逻辑类似于:
static void timer_cb(evutil_socket_t fd, short what, void *arg)
{
// do some checking
...
}
int main(int argc, char* argv[])
{
struct event *foo_timer;
...
// set up libvent timer to be executed every 1 second
foo_timer = event_new(base, -1, EV_TIMEOUT|EV_PERSIST, timer_cb, args);
event_add(foo_timer, &one_sec);
// dispatch event
...
}
因此回调函数将每1秒执行一次。
现在我的问题是,如何动态更改间隔?
例如,假设有另一个线程向此计时器回调发送信号(不知道何时),例如设置布尔flag
。
我要实现的是,当回调发现flag
是true
时,它可以将时间间隔更改为500ms。
这样,计时器回调将在当前回调完成后的500毫秒后执行。
我现在停留在这里,因为我发现的唯一可能方法是删除该计时器并创建(添加)一个新计时器。 喜欢:
static void timer_cb(evutil_socket_t fd, short what, void *arg)
{
// do some checking
...
// create new event and remove the old one
if (flag)
{
// create new timer
bar_timer = event_new(base, -1, EV_TIMEOUT|EV_PERSIST, timer_cb, bar_timer);
event_add(bar_timer, &five_100ms);
// remove old one
old_event = (struct event *)args;
event_del(old_event);
}
}
int main(int argc, char* argv[])
{
struct event *foo_timer;
...
// set up libvent timer to be executed every 1s
foo_timer = event_new(base, -1, EV_TIMEOUT|EV_PERSIST, timer_cb, foo_timer);
event_add(foo_timer, &one_sec);
// dispatch
...
}
但是我在event_del(old_event);
之后崩溃了
如果我放弃event_del
,则似乎未触发新事件。
所以我需要一些有关如何实现目标的建议,还是应该转向其他库来实现这一目标?
答案 0 :(得分:0)
是,唯一的方法是删除旧事件并创建具有500ms超时的新事件。 检查指针(old_event)是否有效。
Option Explicit
Dim lastR As Long
Dim lastC As Long
Dim startCell As Range
Dim tblSource As Range
Dim wsSource As Worksheet
Dim wb As Workbook
Sub dashRefresh()
'
'Refresh Excel table with newest Oracle data
'
Set wb = Workbooks("OTR_Dashboard")
Set wsSource = wb.Sheets("RawData")
With wsSource
Set startCell = Range("A1")
lastR = wsSource.Cells(Rows.Count, 1).End(xlUp).Row
lastC = wsSource.Cells(3, Columns.Count).End(xlToLeft).Column
End With
Set tblSource = wsSource.Range(wsSource.Cells(1, 1), wsSource.Cells(lastR, lastC))
With ActiveWorkbook.Names("dataTbl")
.Name = "dataTbl"
.RefersTo = tblSource
.Comment = ""
End With
Application.CutCopyMode = False
Sheets("DASHBOARD").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveWorkbook.RefreshAll
End Sub
我的建议是使用Libevent 2.1.1-alpha版本中可用的event_self_cbarg。