Click here to Skip to main content
16,015,351 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
class SQLJob
    {


        #region User Variables

        const int timeoutInterval = 60;//Set Timeout in seconds
        static readonly string SqlServer = @"Server112\Dev,20481"; //set SqlServer, may use instance/port too eg: USPLSVUL156\Operations,20481
        static readonly string SqlAgentJobName = "Some_SqlAgentJob_J250"; //set name of the job to fire

        #endregion


        static bool loopContinuity = false;
        static Timer stateTimer;
        static int CurrentRunRetryAttempt = 0;
        static ServerConnection conn;
        static Server server;
        static Job job;

        public static void Main(string[] args)
        {
            //Enable Timer
            SetTimer();
            try
            {
                conn = new ServerConnection(SqlServer); //Create SQL server conn, Windows Authentication
                server = new Server(conn); //Connect SQL Server
                job = server.JobServer.Jobs[SqlAgentJobName]; //Get the specified job
                StartJob();
            }
            catch (Exception ex)
            {
                SetTimer(true);
                Console.WriteLine("Failed to start the job :" + ex.Message);
                throw ex;
            }
            finally
            {
                Destroyobjects();
            }

        }

        static void Destroyobjects()
        {
            if (job != null)
                job = null;
            if (server != null)
                server = null;
            if (conn != null)
            {
                conn.Disconnect();
                conn = null;
            }
        }

        private static void SetTimer(bool cancel = false)
        {
            if (!cancel)//Initiate
            {
                stateTimer = new Timer(timeoutInterval * 1000);//Set Timeout interval as timeoutInterval
                stateTimer.Enabled = true;//Enable timer
                stateTimer.Elapsed += new ElapsedEventHandler(Tick); //Set timer elapsed event handler

            }
            else //Disable timer
            {
                if (stateTimer != null)
                    stateTimer.Dispose();

            }
        }

        static public void Tick(object source, ElapsedEventArgs e)
        {

            loopContinuity = true;//normal stop...
            Console.WriteLine(string.Format("Timeout reached at {0){1}CurrentRunRetryAttempt={2}", e.SignalTime, Environment.NewLine, CurrentRunRetryAttempt));
            throw new Exception(string.Format("Timeout reached at {0){1}CurrentRunRetryAttempt={2}", e.SignalTime, Environment.NewLine, CurrentRunRetryAttempt));// comment this line if we do not want an abrupt stop
        }

        static void StartJob()
        {

            try
            {
                while (loopContinuity == false) //Wait till the job is idle
                {
                    job.Refresh();
                    if (job.CurrentRunStatus == JobExecutionStatus.Executing) //Check Job status and find if it’s running now
                    {
                        CurrentRunRetryAttempt++;
                        //We are not ready to fire the job
                        loopContinuity = false;
                        System.Threading.Thread.Sleep(10 * 1000); //Wait 10 secs before we proceed to check it again.
                    }
                    else
                    {
                        //We are ready to fire the job
                        loopContinuity = true; //Set loop exit
                        try
                        {
                            job.Start();//Start the job
                            SetTimer(true);//disable timer if we are able to start the job, i.e. there’s no exception on starting the job.
                        }
                        catch
                        {
                            loopContinuity = false; //Fail to start, continue to loop.
                            System.Threading.Thread.Sleep(10 * 1000); //Fail to start, wait 10 seconds and try again
                        }

                    }
                    string s = "CurrentRunStatus=" + job.CurrentRunStatus.ToString();
                    Console.WriteLine(s); //Print status             

                }
            }
            catch
            {
                throw;
            }
        }
    }

})

I have added all required name spaces to the form.
Posted
Updated 7-Nov-22 5:30am
v3
Comments
skydger 2-Oct-12 3:49am    
And the question is?..
Manfred Rudolf Bihy 2-Oct-12 3:50am    
Your wrote: "I have added all required name spaces to the form.", but your forgot to tell us what went wrong and what your issues or question/s are in this regards.
Please add the nescessary details of your problem.

Thanks!

1 solution

First you need to go to Project Properties and change the "Output" to Windows Application.

Second step - you need to add a Form.
In main entry point, load the newly created form.

Take all the codes from main entry point and add it in form_load event.

If you need tyo add control, you can add to win form and update the code.
Compile and you are done.

Best of luck!

cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900