Hi
im launching a dsql agent job via SMO JobServer.Jobs class and issuing job.Start() which works fine.
I then want to wait in a loop updating a textbox or label or something and inside the loop im checking the jobexecutionstatus is executing.
The problem is it just launches the job and never waits, has anybody else doen similar to this and can help please
Tuhin PaulPosted Oct 16, 2024, 4:21 AM
To check and wait for an SQL Server Agent job status using the SQL Server Management Objects (SMO) in C#, you can follow this approach. Essentially, after starting the job, you can repeatedly check the status of the job in a loop. You can update the UI elements like a textbox or label to show the job's status, but you also need to make sure you're not blocking the UI thread.
The Server object is used to connect to the SQL Server and interact with the SQL Agent jobs. It connects via SMO using ServerConnection. The job.Start() method launches the job, and we then enter a loop to monitor its status. The MonitorJobAsync() method uses a while (true) loop to continuously check the job’s status until it completes or fails. The job.Refresh() call is important as it refreshes the job's status. The statusLabel.Text is updated with the current job status in the loop. Since this is done asynchronously, the UI won’t be blocked. A Task.Delay(3000) ensures that the status is polled every 3 seconds, but you can adjust this delay as per your requirements.
Perry WhittlePosted Oct 16, 2024, 1:21 PM
thanks, that works perfectly
Perry WhittlePosted Oct 16, 2024, 9:39 AM
Hi Tuhin
thank you for the code listing, the Jobexecution status does not expose succeeded or failed.
There are some other oddities such as with the await job sync call which i'll look into