- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序中有很多按钮。它们彼此相邻放置。所有方法都是 IMvxAsyncCommand 类型。经过用户测试后,我发现了一些不匹配的地方。我发现重复的操作 - 几乎同时调用两个不同的按钮。
我所做的就是创建自己的 SafeAsyncCommand 类并继承自 MvxAsyncCommand。我的目标是在执行之间创建延迟 - 我想在低于 0.5 秒的情况下防止给定延迟中的双击。
这是我的工作:
public static class SafeCommandSettings
{
public static bool CanExecute { get; private set; }
public static TimeSpan Delay => TimeSpan.FromMilliseconds(500);
static SafeCommandSettings()
{
CanExecute = true;
}
public static async void Pause()
{
if (!CanExecute) return;
CanExecute = false;
await Task.Delay(Delay);
CanExecute = true;
}
}
public class SafeAsyncCommand : MvxAsyncCommand
{
public SafeAsyncCommand(Func<Task> execute, Func<bool> canExecute = null, bool allowConcurrentExecutions = false)
: base(execute, canExecute, allowConcurrentExecutions)
{
}
public SafeAsyncCommand(Func<CancellationToken, Task> execute, Func<bool> canExecute = null, bool allowConcurrentExecutions = false)
: base(execute, canExecute, allowConcurrentExecutions)
{
}
protected override async Task ExecuteAsyncImpl(object parameter)
{
if (!SafeCommandSettings.CanExecute) return;
SafeCommandSettings.Pause();
await base.ExecuteAsyncImpl(parameter);
}
}
public class SafeAsyncCommand<T> : MvxAsyncCommand<T>
{
public SafeAsyncCommand(Func<T, Task> execute, Func<T, bool> canExecute = null, bool allowConcurrentExecutions = false)
: base(execute, canExecute, allowConcurrentExecutions)
{
}
public SafeAsyncCommand(Func<T, CancellationToken, Task> execute, Func<T, bool> canExecute = null, bool allowConcurrentExecutions = false)
: base(execute, canExecute, allowConcurrentExecutions)
{
}
protected override async Task ExecuteAsyncImpl(object parameter)
{
if (!SafeCommandSettings.CanExecute) return;
SafeCommandSettings.Pause();
await base.ExecuteAsyncImpl(parameter);
}
}
我认为这可行,但我看到用户能够再次执行此操作。我是否错过了一些有关异步方法或静态线程安全的知识?
提前致谢
最佳答案
为此,您可以利用 MvxNotifyTask这是 Task
的包装器,用于监视不同的任务状态,您将运行命令并执行类似的操作(请注意,您不需要命令为 MvxAsyncCommand
):
public MvxNotifyTask MyNotifyTaskWrapper { get; private set; }
public MvxCommand MyCommand { get; private set; }
private void InitializeCommands()
{
// this command is executed only if the task has not started (its wrapper is null) or the task is not in progress (its wrapper is not IsNotCompleted)
this.MyCommand = new MvxCommand(() => this.MyNotifyTaskWrapper = MvxNotifyTask.Create(() => this.MyLogicAsync()),
() => this.MyNotifyTaskWrapper == null || !this.MyNotifyTaskWrapper.IsNotCompleted);
}
private async Task MyLogicAsync()
{
// my async logic
}
因此,一旦异步进程启动,该命令就无法再次执行,以防止重复操作,并且您可以在该任务完成后再次启动它。
如果您在运行某些任务时必须禁用多个命令执行,只需在不同命令上添加相同的 CanExecute
条件或混合不同 MvxNotifyTask
的条件
还要检查 MvxNotifyTask
是否会引发属性更改通知,您可以在 View 中订阅或绑定(bind)该通知,并在执行操作时显示“正在加载”或类似内容。
注意:如果您使用的是 Mvx < 5.5,则不会有 MvxNotifyTask
,但您可以使用 NotifyTaskCompletion由 Stephen Cleary 完成,与 MvxNotifyTask
几乎相同,并且它源自 MvxNotifyTask
的基础。
嗨
关于asynchronous - MvvmCross异步命令锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49368786/
来自 Akka 文档,Pipelining and Parallelism Akka Streams processing stages (be it simple operators on Flow
我正在开发一个提取元数据的 chrome 扩展程序。解析元数据的代码包含在内容脚本中。 background.js 和 content.js 通过 sendMessage 请求和响应进行通信。我遇到了
我正在使用 Python 3.7.4 和这段代码(MWE): import asyncio async def foo(x): await asyncio.sleep(1) retur
嘿,我对 Dart Futures 很陌生,我有以下情况。 每当用户在 UI 中键入一个字母时,addressChanged()我的 ui_component 中的方法被调用。该方法调用方法getPr
我在尝试将异步函数转换为同步函数时遇到问题。 这是类中的一个方法: doPost: function(call, data) { var uri = 'http://localhost/api
在很多关于 C# 的 async/await 的讨论中,我看到人们提到了“自然异步”或“纯异步”的术语。这些术语到底是什么意思? “自然异步”操作的一些示例是什么?为什么这样调用它? “非自然异步”操
现在我正在使用 Gevent,我想问两个问题: 有没有办法执行特定的任务,这将 从不异步执行 (而不是在每个任务中使用锁) 有没有办法到优先在 Gevent 中生成任务?就像一组将以低优先级生成的任务
在 document , 如果方法也用@gen.coroutine 修饰,则不需要@web.asynchronous。像这样 @web.asynchronous @gen.coroutine def
已在 Google Analytics(分析)帮助论坛中发布此内容,但无人能提供帮助。希望我在这里有更多的运气......: 我对我的网页使用 Google Anlaytics 异步跟踪。像个魔法一样
我正在尝试从 Controller 异步发送电子邮件并收到以下错误: 我不想等待电子邮件发送完成操作。 An asynchronous module or handler completed whil
在使用 SendMailAsync 时出现以下错误: An asynchronous module or handler completed while an asynchronous operati
我有一个非常简单的 ASP.NET MVC 4 Controller : public class HomeController : Controller { private const st
我正在编写一个使用 ASP.NET Web API 代理一些 HTTP 请求的应用程序,我正在努力识别间歇性错误的来源。这似乎是一个竞争条件...但我不完全确定。 在详细介绍之前,先介绍应用程序的一般
Cancel CancellationTokenSource 的成员对象“传达取消请求”,我认为这意味着它是触发并忘记并且不会等到取消完成(例如,所有异常处理程序都已运行)。这很好,但我需要等到一个未
在 D 中异步调用其他进程的首选方法是什么?我的用例正在调用 svn status检查退出状态,并解析其标准输出和错误。 最佳答案 我想 std.stdio.popen是你想要的: void pope
我一直听说使用异步编程模式会使我的代码运行得更快。为什么这是真的?无论是现在运行还是稍后运行,都不是必须以任何一种方式运行完全相同的代码吗? 最佳答案 它不是更快,它只是不浪费时间。 同步代码在等待
我有点困惑为什么同步调用与异步调用不同,因为从来没有“立即”响应,它仍然需要几纳秒或几毫秒? 最佳答案 同步调用在完成其工作(或达到超时)后返回其调用者。异步调用在启动其他事件后立即返回。 这意味着,
我正在尝试使用 MSDN 上描述的 OVERLAPPED 结构异步调用 DeviceIO 函数。 我正在使用 FSCTL_ENUM_USN_DATA 控制代码来枚举 NTFS 驱动器的 MFT,但我无
我一直在尝试创建一个服务器进程,以异步方式从客户端进程接收输入文件路径和输出路径。服务器进行了一些与数据库有关的转换,但是为了简单起见,我们假设它只是将所有内容都转换为大写。这是服务器的一个玩具示例:
我正在编写一个异步方法,它应该异步查询一个端口,直到找到一个,或者在 5 分钟后超时; member this.GetPort(): Async = this._GetPort(DateTim
我是一名优秀的程序员,十分优秀!