You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Identify函数实现指纹认证与按键取消的互斥等待?

解决方案:同时监听指纹认证与按键取消输入

Got it, let's solve this problem where we need to wait for either a fingerprint input for authentication OR a key press to cancel the process. Here's how to modify your Identify function to handle both operations in parallel, with one triggering the cancellation of the other as soon as it completes.

Modified Code

using System.Threading;
using System.Threading.Tasks;

public static bool Identify(out FingerId identity)
{
    identity = default;
    var cancellationTokenSource = new CancellationTokenSource();
    bool wasInterruptedByKeyPress = false;

    // Task 1: Run fingerprint authentication (supports cancellation)
    var fingerprintTask = Task.Run(() =>
    {
        try
        {
            // Replace with your actual FingerBio.Identify call
            // If your FingerBio API accepts a CancellationToken, pass it here
            bool authSuccess = FingerBio.Identify(out identity, cancellationTokenSource.Token);
            return authSuccess;
        }
        catch (OperationCanceledException)
        {
            // Authentication was canceled by key press
            return false;
        }
    }, cancellationTokenSource.Token);

    // Task 2: Listen for any key press to cancel authentication
    var keyPressTask = Task.Run(() =>
    {
        Console.Write("Enter any key to cancel. ");
        Console.ReadKey(true); // 'true' hides the pressed key from console output
        wasInterruptedByKeyPress = true;
        cancellationTokenSource.Cancel(); // Cancel the fingerprint task immediately
        return true;
    }, cancellationTokenSource.Token);

    // Wait for the first task to finish (either fingerprint auth completes or key is pressed)
    var completedTask = Task.WhenAny(fingerprintTask, keyPressTask).Result;

    // Clean up: Ensure both tasks are properly canceled/finished to avoid resource leaks
    cancellationTokenSource.Cancel();
    Task.WaitAll(fingerprintTask, keyPressTask);

    // Handle the result
    if (wasInterruptedByKeyPress)
    {
        Console.WriteLine("\nFingerprint authentication canceled.");
        return false;
    }

    return fingerprintTask.Result;
}

Key Details & Notes

  • Cancellation Token: We use CancellationTokenSource to create a cancelable context. When either task finishes, it cancels the other task to stop unnecessary work immediately.
  • Fingerprint Task Adjustment: If your FingerBio.Identify method doesn't natively support cancellation tokens, you'll need to tweak the logic—for example, add periodic checks of cancellationTokenSource.Token.IsCancellationRequested inside any loops in the fingerprint process to exit early.
  • Key Press Behavior: Console.ReadKey(true) prevents the pressed key from being displayed in the console, keeping the output clean. Remove the true parameter if you want to show the key the user pressed.
  • Cleanup: Always call Cancel() and WaitAll() to ensure no background threads are left hanging, which could cause memory leaks or unexpected behavior.
  • Exception Handling: We catch OperationCanceledException to gracefully handle cases where the fingerprint task is canceled by the key press, avoiding unhandled exceptions.

内容的提问来源于stack exchange,提问作者Administrator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:13:00