I have the application In which i am ruuning the procedure which runs the Sql Job
and at the other end i need to show the job history in grid whose query is select job_id, step_id, step_name, message, run_date from sysjobhistory
where run_date = replace(convert(varchar(10), getdate(), 121),'-','')

the job which i am running internally runs many job
I want to show job status one by one in grid not after the procedure fully executed
Till now i have not done Thread Programming
Search a lot but didnt got the same

I have done rnd and did the part but its giving me following exception
cross thread operation not valid: control datagridview1 access from a thread other than the thread it was created on/B]
I am not getting what the error is indicating

Folloimg is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;


namespace JobSqlStart
{
    public partial class Form1 : Form
    {
        public string status = "";      //flag string to refresh the data in the gridview
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }//private void Form1_Load(object sender, EventArgs e)

        private void Run_Jobbutton_Click(object sender, EventArgs e)
        {
            Thread runproc = new Thread(new ThreadStart(RunProc));
            Thread othread = new Thread(new ThreadStart(ShowData_in_Grid));
            runproc.IsBackground = true;
            runproc.Start();
            othread.Start();

        }//private void Run_Jobbutton_Click(object sender, EventArgs e)



        public void ShowData_in_Grid()
        {
            DataSet ds = new DataSet();
            string query = "select job_id, step_id, step_name, message, run_date, run_time from sysjobhistory where run_date = replace(convert(varchar(10), getdate(), 121),'-','')";
            SqlConnection connstr = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["database1"]);
            SqlCommand cmd = new SqlCommand(query, connstr);
            try
            {
                connstr.Open();
                while (status != "success")
                {
                    ds.Clear();
                    dataGridView1.DataSource = null;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
                ds.Clear();
                dataGridView1.DataSource = null;
                SqlDataAdapter da1 = new SqlDataAdapter(cmd);
                da1.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];


            }
            catch (Exception err)
            {
                MessageBox.Show("Error : " + err.Message);
            }

            finally
            {
                connstr.Close();
            }
        }   //public void ShowData_in_Grid()



        public void RunProc()
        {
            //MessageBox.Show("RunProc also called");
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["database"]);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("RUNSQLJOB", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();
                status = "success";

            }
            catch (Exception err)
            {
                MessageBox.Show("Error : " + err.Message);
            }
            finally
            {
                conn.Close();
            }
        }//public void RunProc()


    }
}
This article has been dead for over six months. Start a new discussion instead.