状态的后台验证,根据用户操作重置 [英] Background validation of state, reset on user action

查看:90
本文介绍了状态的后台验证,根据用户操作重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是多线程/多线程线程处理的新手,但是我正在从事一个我认为需要它的项目.用户将编辑一个相当复杂的图,并且我希望程序检查该图的有效性.有效性检查是很简单的(虽然多项式时间不是NP-秒,不是分钟或几年,但是我不想在每次更改后让用户保持几秒钟的时间),所以我希望程序进行检查以确保背景中的有效性,并在发现不一致之处时突出显示不一致之处.当用户对图进行某些更改(更改结构,而不仅仅是更改元素上的标签)时,验证将不得不放弃正在执行的操作并重新开始.我假设用户最终会休息一下,想和那个可爱的人两个小隔间想一想,去小便/去喝咖啡/聊天,但如果他们不这样做,我会拥有在某些情况下(例如在保存或打印输出之前)使验证运行完成.广泛应用,我需要学习C#的哪些功能,以及如何构造它?

I'm really new to threading multitasking/multithreading, but I'm working on a project where I think I need it. The user will be editing a fairly complex diagram, and I want the program to check for validity of the diagram. The validity check is non-trivial (polynomial time, though, not NP - seconds, not minutes or years, but I don't want to hold the user up for a few seconds after every change) so I would like the program to check for validity in the background and highlight inconsistencies when it finds them. When the user makes certain changes to the diagram (changes the structure, not just the labels on elements), the validation will have to throw away what it was doing and start again. I'm assuming the user will eventually take a break to think/go for a pee/go for a coffee/chat to that rather cute person two cubicles along, but in case they don't, I have to let the validation run to completion in some circumstances (before a save or a printout, for example). Broad-brush, what are the features of C# I'll need to learn, and how do I structure that?

推荐答案

宽刷.我们走了.

问:我需要学习C#的哪些功能?"

Q: "What are the features of C# I'll need to learn?"

A:您可以轻松地使用一个基本工具箱,大致由以下组成:

A: You can get by nicely with a basic toolkit consisting (roughly speaking) of:

System.Threading.Tasks.Task
System.Threading.CancellationTokenSource
System.Threading.SemaphoreSlim

问:我不想让用户在每次更改后都停留几秒钟"

Q: "I don't want to hold the user up for a few seconds after every change"

A:好的,所以我们永远不会阻塞UI线程.触发任务"以运行后台验证例程,该例程会不时检查一次,以查看是否已取消.

A: OK, so we will never-ever block the UI thread. Fire off a Task to run a background validation routine that checks every now and then to see if it's been cancelled.

CancellationTokenSource _cts = null;
SemaphoreSlim ssBusy = new SemaphoreSlim(2);
private void ExecValidityCheck()
{
    ssBusy.Wait();
    Task.Run(() =>
    {
        try
        {
            _cts = new CancellationTokenSource();
            LongRunningValidation(_cts.Token);
        }
        finally
        {
            ssBusy.Release();
        }
    })
    .GetAwaiter()
    .OnCompleted(CheckForRestart);
}

我们将使用GetAwaiter().OnCompleted()调用 CheckForRestart .这只是意味着无阻塞,由于以下三个原因之一,当线程结束时,我们将收到回调通知:

We'll call CheckForRestart using GetAwaiter().OnCompleted(). This just means that without blocking we'll be notified as a callback when the thread finishes for one of three reasons:

  1. 已取消
  2. 已取消,但打算从头开始进行验证.
  3. 通过验证完成

通过调用 CheckForRestart ,我们确定是否重新启动.

By calling CheckForRestart we determine whether to start it over again or not.

void CheckForRestart()
{
    BeginInvoke((MethodInvoker)delegate
    {
        if (_restart)
        {
            _restart = false;
            ExecValidityCheck();
        }
        else
        {
            buttonCancel.Enabled = false;
        }
    });
}

我将完整的代码发布在这里,而是将一个简单的工作示例推送到我们的GitHub.您可以在那里浏览或克隆并运行它. 20秒的屏幕捕获.在视频中单击重新启动"按钮时,它将检查信号量的 CurrentCount 属性.它以线程安全的方式确定验证例程是否已在运行.

Rather that post the complete code here, I pushed a simple working example to our GitHub. You can browse it there or clone and run it. 20-second screen capture. When the RESTART button is clicked in the video, it's checking the CurrentCount property of the Semaphore. In a threadsafe way it determines whether the validation routine is already running or not.

希望我能给您一些有关从哪里开始的想法.当然,我在这里给出的解释有一些漏洞,但是请随时在评论中解决您的关键问题,我将尽力予以答复.

I hope I've managed to give you a few ideas about where to start. Sure, the explanation I've given here has a few holes but feel free to address your critical concerns in the comments and I'll try to respond.

这篇关于状态的后台验证,根据用户操作重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆