Leo, I used a similar approach some years back but then found a better way...it makes use of xp_sqlagent_enum_jobs which gives the actual running status of a job (among other things).
if (select object_id('tempdb.dbo.#jobstauts')) > 0
drop table dbo.#jobstauts
create table #jobstauts(
JobID UNIQUEIDENTIFIER null,
LastRunDate int null,
LastRunTime int null,
NextRunDate int null,
NextRunTime int null,
NextRunScheduleID int null,
RequestedToRun int null,
RequestSource int null,
RequestSourceID sysname null,
Running int null,
CurrentStep int null,
CurrentRetryAttempt int null,
State int null
)
declare @jobid UNIQUEIDENTIFIER
set @jobid = convert(UNIQUEIDENTIFIER,(select job_id
from msdb.dbo.sysjobs
where name = ''))
insert into dbo.#jobstauts
exec master.dbo.xp_sqlagent_enum_jobs 1, 'sa', @jobid
if (select Running from dbo.#jobstauts) = 1 -- running
Begin
(additional code>
End