由于 C# 中的计时器而导致线程排队

Queuing up of threads because of timer in C#

在我的应用程序中,我使用了线程。所以我有一个名为 pull 的函数,它在一个单独的线程中运行。

var timerCall = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1));
   _subscription = timerCall.Subscribe(async res =>
        {
            List<dynamic> result = pull();

            if (result != null && result.Count > 0)
                await Connection.Broadcast(new
                {
                    id = _newestId,
                    events = result
                });
        });

每一秒我都调用线程。我也在我的 pull 函数中使用 lock,就像这样

lock (_locker){
       // do stuff 
}

让我们假设函数 pull 每次调用平均需要 1.2 秒,所以这意味着最终我会得到一个大队列。我不需要那个。有什么方法可以预防吗?

我能想到一件事。您可以使用 Monitor Class 来检查是否获取了锁。所以你可以跳过线程,如果它仍然很忙,再等一秒钟,直到另一个线程完成。