无视排队鼠标事件鼠标、事件

2023-09-03 13:08:51 作者:坚持下去便成王

我有一个应用程序用C#编写面向.NET Compact Framework的3.5,运行在Windows CE。不时,操作持续一秒钟左右,正在UI线程上执行。我当前设置的 Cursor.Current 属性表明应用程序正忙,但这并不起价​​急于用户p $ pvent鼠标事件排队。这有时会导致意想不到的点击。

I have an application written in C# targeting .NET Compact Framework 3.5, running on Windows CE. From time to time, operations lasting for a second or so are being performed on the UI thread. I currently set the Cursor.Current property to indicate that the application is busy, but this does not prevent mouse events from eager users to queue up. This sometimes leads to unintended clicks.

什么是忽略.NET Compact Framework的平台上排队鼠标消息的最佳方式是什么?可悲的是,code必须在UI线程上运行。

What is the best way to ignore queued mouse messages on the .NET Compact Framework platform? Sadly, the code has to run on the UI thread.

推荐答案

禁用的控制不会帮你,因为我发现从我的POS应用程序,用户可以在另一个点击忙里偷闲50ms左右,尤其是当使用未校准触摸屏。

Disabling the controls won't help you, as I've found from my POS application that the users can sneak in another click in about 50ms, especially when using a touch screen that is not calibrated.

一个这创造是生产发票的时候,你不能有问题,重复的点击产生另一张发票,只是因为有清除当前发票之前,50ms的延迟。

One of the problems this creates is when producing an invoice, you can't have a duplicate click produce another invoice, just because there's a 50ms delay before clearing the current invoice.

在这样的情况下,我用一个类似的模式:

In cases like this, I use a pattern similar to this:

    public static void ClearMouseClickQueue()
    {
        Message message;
        while (PeekMessage(out message,IntPtr.Zero, (uint) MessageCodes.WM_MOUSEFIRST,(uint) MessageCodes.WM_MOUSELAST,1) != 0)
        {    
        }
    }

    private object approvalLockObject = new object();

    private void btnApproveTransaction_Click(object sender, EventArgs e)
    {
        ApproveTransactionAndLockForm();
    }

    private void ApproveTransactionAndLockForm()
    {
        lock (approvalLockObject)
        {
            if (ApprovalLockCount == 0)
            {
                ApprovalLockCount++;
                ApproveTransaction();
            }
            else
            {
                CloseAndRetry();
            }
        }
    }

    private void ApproveTransaction()
    {
        ClearMouseClickQueue();

        this.Enabled = false;

        Logger.LogInfo("Before approve transaction");

        MouseHelper.SetCursorToWaitCursor();

        ... validate invoice and print
    }

在情况下,你需要重新启用该屏幕上,做到这一点:

In case you need to reenable the screen, do this:

            this.Enabled = true;

            ApprovalLockCount = 0;

            DialogResult = DialogResult.None;