首页 > 解决方案 > 带有 WAITFOR DELAY 的过程直到最后才会执行

问题描述

我有一个程序,基本上用于某个任务的超时。用户必须在 3 分钟内完成此任务,否则他们将失败。这是它的样子:

--Perform an insert statement that notifies the user that they have 3 minutes to finish this task
WAITFOR DELAY '00:00:30'
IF EXISTS(SELECT*FROM _Users WHERE UserID=@UserID AND isDone=0)
--Perform another insert statement that notifies the user that they have 2 minutes 30 seconds remaining to finish this task
WAITFOR DELAY '00:00:30'
IF EXISTS(SELECT*FROM _Users WHERE UserID=@UserID AND isDone=0)
--Perform another insert statement that notifies the user that they have 2 minutes remaining to finish this task
WAITFOR DELAY '00:00:30'
IF EXISTS(SELECT*FROM _Users WHERE UserID=@UserID AND isDone=0)
--Perform another insert statement that notifies the user that they have 1 minute 30 seconds remaining to finish this task
WAITFOR DELAY '00:00:30'
IF EXISTS(SELECT*FROM _Users WHERE UserID=@UserID AND isDone=0)
--Perform another insert statement that notifies the user that they have 1 minute remaining to finish this task
WAITFOR DELAY '00:00:30'
IF EXISTS(SELECT*FROM _Users WHERE UserID=@UserID AND isDone=0)
--Perform another insert statement that notifies the user that they have 30 seconds remaining to finish this task
WAITFOR DELAY '00:00:20'
IF EXISTS(SELECT*FROM _Users WHERE UserID=@UserID AND isDone=0)
--Perform another insert statement that notifies the user that they have 10 seconds remaining to finish this task
WAITFOR DELAY '00:00:10'
IF EXISTS(SELECT*FROM _Users WHERE UserID=@UserID AND isDone=0)
--Perform another insert statement that notifies the user that they have failed to complete the task within the required time frame. 

但由于某种原因,程序总是在剩下 1 分钟时停止。我注意到,当我手动执行此过程时,它工作得很好,但是当我通过另一个过程也由另一个过程执行的另一个过程执行时,它会在剩下 1 分钟时停止。

有什么我做错了,有没有更好的方法来做到这一点?

PS:我不拥有执行这个程序的应用程序的源代码,我也不能要求任何修改来处理源代码中的超时,因此,我必须用一个程序来做。

标签: sql-server

解决方案


推荐阅读