动作延迟扩展方法不起作用
我正在尝试为 Action 创建一个方便的扩展方法,基本上在延迟后运行该操作: 到目前为止,我的扩展看起来像这样
public static void DelayAction(this Action DelayedAction, int millisecondDelay, CancellationToken Token)
{
Task t = Task.Factory.StartNew(() => { Thread.Sleep(millisecondDelay); }, Token, TaskCreationOptions.None, TaskScheduler.Default);
t.ContinueWith(_ => DelayedAction, Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
我有一个我调用的函数,它依次使用这些扩展
private void DelayTask(Action ActiontoDelay, int millisecondDelay)
{
ActiontoDelay.DelayAction(millisecondDelay, _TokenSource.Token);
}
我这样调用:
DelayTask(() => { _SomeFunction(SomeArgs); }, 1500);
但这一切似乎放下一个整体,动作永远不会发生。我哪里错了?
编辑 17-11-11 2300hrs:
我删除了通用扩展方法,因为它与本示例无关。
还在这里发布评论,因为它没有在注释中清楚地格式化代码
如果我直接这样做而不是调用
DelayTask(() => { _SomeFunction(SomeArgs); }, 1500);
:
Task t = Task.Factory.StartNew(() => { Thread.Sleep(1500); }, Token, TaskCreationOptions.None, TaskScheduler.Default);
t.ContinueWith(() => { _SomeFunction(SomeArgs); }, Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
它工作正常(抱歉,如果有任何语法错误,我已经从内存中完成了)所以我相信我的问题在于尼克·巴特勒的答案回避的动作的处理
I'm trying to create a handy extension method to Action to basically run that action after a delay: So far my extension looks like this
public static void DelayAction(this Action DelayedAction, int millisecondDelay, CancellationToken Token)
{
Task t = Task.Factory.StartNew(() => { Thread.Sleep(millisecondDelay); }, Token, TaskCreationOptions.None, TaskScheduler.Default);
t.ContinueWith(_ => DelayedAction, Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
I have a function I call that in turn uses these extensions
private void DelayTask(Action ActiontoDelay, int millisecondDelay)
{
ActiontoDelay.DelayAction(millisecondDelay, _TokenSource.Token);
}
Which I call like this:
DelayTask(() => { _SomeFunction(SomeArgs); }, 1500);
But it all seems to drop down a whole and the action never fires. Where am I going wrong?
Edit 17-11-11 2300hrs:
I removed the generic extension method as it's not relevant to this example.
Also posting comment here as it doesn't format the code clearly in comments
If instead of the call
DelayTask(() => { _SomeFunction(SomeArgs); }, 1500);
I do this directly:
Task t = Task.Factory.StartNew(() => { Thread.Sleep(1500); }, Token, TaskCreationOptions.None, TaskScheduler.Default);
t.ContinueWith(() => { _SomeFunction(SomeArgs); }, Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
It works ok (sorry if there's any syntax error there I have done it from memory) So I beleive my issue is in the handling of the Action which Nick Butler's Answer eludes to
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的延续将返回 DelayedAction 委托,而不是调用它:
Your continuations are returning the
DelayedAction
delegate, not invoking it:如果任务不是一个强烈的要求,我建议使用 System.Threading。内置“延迟”功能的计时器,构造函数如下:
MSDN:
! 另外,重要的是您可以在构建阶段之后更改此延迟。
If Tasks is not a strong requirement I would suggest use System.Threading.Timer which has built in "delay" feature, constructor looks like:
MSDN:
! Also what is important you can change this delay after the construction stage.
当然,你可以通过任务来完成。问题是,您调用了 TaskFactory 的 StartNew,它会立即启动您的任务。
以这种方式尝试 - 这应该有效:
Of course you can do it with tasks. The problem was, that you called StartNew of TaskFactory, which immediatley starts your task.
Try it in this way - this should work: