简体   繁体   English

如何在C#中使用后台工作程序类执行存储过程

[英]how to use background worker class for stored procedure execution in c#

I have the below code in C# where i'm trying to use the background worker class to display the progress of execution of a stored procedure. 我在C#中有以下代码,其中我正在尝试使用后台工作程序类显示存储过程的执行进度。

Or(any input how to display the message of progress while stored procedure execution via c# is much appreciated) 或(非常感谢通过c#执行存储过程执行时如何显示进度消息的任何输入)

Problem is when i click the button there is no progress of execution in the code dont know where the issue occurs. 问题是,当我单击按钮时,代码中没有执行进度,不知道问题出在哪里。 Let me know if anyone has some ideas. 让我知道是否有人有想法。

Thanks 谢谢

//Butoon click code
 private void button_executeBL_Click(object sender, EventArgs e)
    {
        if (!backgroundWorker1.IsBusy)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        else
        {
            label1.Text = "Busy Processing, Please wait";
        }

    }

    //background worker class process...

   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(200);
        int count =1;
        string connectionString = "Data Source=.;Initial     Catalog=VegetablesCoSD;Integrated Security=True";
        string commandText = "CoSD.BusinessLogic";

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand(commandText, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 600;
            try
            {
                conn.Open();

                //count will be the number of rows updated. will be zero if no rows updated.
                 backgroundWorker1.ReportProgress( count = cmd.ExecuteNonQuery());

                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    backgroundWorker1.ReportProgress(0);
                    return;
                }

                e.Result = count;
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Update Failed coz.. " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }


    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label1.Text = e.ProgressPercentage.ToString();

    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
        {
            label1.Text = "Process Cancelled";
        }
        else if (e.Error != null)
        {
            label1.Text = e.Error.Message;
        }

        else
        {
            MessageBox.Show("Business Rules Executed Successfully!!!");
            label1.Text = "Total Records Inserted" + e.Result.ToString();
        }

    }

    private void button_Cancel(object sender, EventArgs e)
    {
        if (backgroundWorker1.IsBusy)
        {
            backgroundWorker1.CancelAsync();
        }

        else
        {
            label1.Text = " No Operation in progress to cancel";
        }


    }

Since you want to run an async task you should change your button click method to async void so you can await your backgroundWorker1.RunWorkerAsync(); 由于要运行异步任务,因此应将按钮的click方法更改为avoid void,以便您可以等待backgroundWorker1.RunWorkerAsync();。

Async methods not being awaited will always run synchronously. 未等待的异步方法将始终同步运行。 Always await async methods to run them asynchronously. 始终等待异步方法以异步方式运行它们。

private async void button_executeBL_Click(object sender, EventArgs e)
{
    if (!backgroundWorker1.IsBusy)
    {
        await backgroundWorker1.RunWorkerAsync();
    }

    else
    {
        label1.Text = "Busy Processing, Please wait";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM