Display progress of execution through progress bar

前端 未结 5 2236
情话喂你
情话喂你 2020-12-28 21:42

I have a silly problem but i am stuck. I am executing a stored procedure form my code procedure takes time so for this I am displaying a progress bar, which shows the progre

5条回答
  •  遥遥无期
    2020-12-28 22:01

    I studied Background worker and i do it this way I put execution of my procedure on another thread and progress bar in GUI thread

     BackgroundWorker bg = new BackgroundWorker();
     Boolean stopwork = true;
     Private void btnYes_Click(object sender, EventArgs e)
            {
                if (DialogResult.Yes == MessageBox.Show(clsGlobalObjectRefrances.OMessageString.SureWant2UpdateMarks, "", MessageBoxButtons.YesNo))
                {
                    try
                    {
    
                        bg.DoWork += bg_DoWork;
                        bg.RunWorkerCompleted += bg_RunWorkerCompleted;
                        bg.RunWorkerAsync();
                        pgbUpdateMarks.Maximum = 60;
                        Stopwatch st = new Stopwatch();
                        st.Start();
                        while(stopwork)
                        {
                            pgbUpdateMarks.Value = st.Elapsed.Seconds;
                        }
                        pgbUpdateMarks.Value = 0;
                        MessageBox.Show("Executed sucessfully");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
    

    Where DoWork is

     void bg_DoWork(object sender, DoWorkEventArgs e)
            {
    
            dbDataEntities db = new dbDataEntities();
            string myquery = "DECLARE @return_value int EXEC    @return_value = [dbo].[ssspUpdateMarksOfStudent] SELECT 'Return Value' = @return_value";
            db.Database.ExecuteSqlCommand("myquery");
            stopwork = false;
            }
    

提交回复
热议问题