c#问题中的进度条和后台工作程序(存储过程) [英] Progressbar and backgroundworker in c# issue (stored procedure)

查看:70
本文介绍了c#问题中的进度条和后台工作程序(存储过程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现ProgressBar并执行存储过程但有一些错误。



I'm trying to implement the ProgressBar and executing a stored procedures but having some errors.

Additional information: Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.







这是我的代码:



frmRunAlert.cs:








Here are my codes:

frmRunAlert.cs:


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

namespace alerts_operations
{
    public partial class frmRunAlert : Form
    {
        DB_Access access = new DB_Access();
        public frmRunAlert()
        {
            InitializeComponent();

            this.backgroundWorker1.WorkerReportsProgress = true;
            this.backgroundWorker1.WorkerSupportsCancellation = true;
        }

        private void frmRunAlert_Load(object sender, EventArgs e)
        {

        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            string today = DateTime.Now.ToString("MM/dd/yyyy");
            dtpRunDate.Text = today;
            dtpAlertDate.Text = today;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (this.backgroundWorker1.IsBusy == true)
            {
                this.backgroundWorker1.CancelAsync();
                this.btnExecute.Enabled = true;
                this.btnClose.Enabled = true;
            }
            else
                this.Close();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            //access.ExecuteAlert(dtpRunDate.Text, dtpAlertDate.Text);
            //MessageBox.Show("ALERTS EXECUTION COMPLETED");
            if (!this.backgroundWorker1.IsBusy)
            {
                this.backgroundWorker1.RunWorkerAsync();
                this.btnExecute.Enabled = false;
                this.btnClose.Enabled = true;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        // On worker thread so do our thing!
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DateTime start = DateTime.Now;
            e.Result = "";
            timer1.Enabled = true;  //Start timer control
            MessageBox.Show("ALERTS STARTING...");
            //access.ExecuteAlert(dtpRunDate.Text, dtpAlertDate.Text);            
            //for (int i = 0; i < 100; i++)
            //{
            //    backgroundWorker1.ReportProgress(i, DateTime.Now);
            //    if (backgroundWorker1.CancellationPending)
            //    {
            //        e.Cancel = true;
            //        return;
            //    }
                access.ExecuteAlert(dtpRunDate.Text, dtpAlertDate.Text);
            //}
            TimeSpan duration = DateTime.Now - start;
            progressBar1.Value = 1800;
            timer1.Enabled = false;
            e.Result = "Successfully Completed. Please check Alert Log! Duration: " + duration.Seconds + "s.";
        }

        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            DateTime time = Convert.ToDateTime(e.UserState);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                MessageBox.Show("The task has been cancelled");
            }
            else if (e.Error != null)
            {
                MessageBox.Show("Error. Details: " + (e.Error as Exception).ToString());
            }
            else {
                MessageBox.Show("The task has been completed. Results: " + e.Result.ToString());
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ////Increment progressBar value by 1 to a max of 1800
            ////progressBar1.Value += 1;
            //Application.Current.Dispatcher.BeginInvoke(
            //DispatcherPriority.backgroundWorker1,
            //new Action(() => this.progressBar1.Value += 1));
        }
    }
}





DB_Access.cs:





DB_Access.cs:

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

namespace alerts_operations
{
    class DB_Access
    {
        SqlConnection conn;
        public DB_Access()
        {
            conn = DB_Connection.GetConnection();
        }

        public void ExecuteAlert(string rundate, string alertdate)
        {
            //try
            //{
                if (conn.State.ToString() == "Closed")
                {
                    conn.Open();
                }
                SqlCommand newCmd = conn.CreateCommand();
                newCmd.Connection = conn;
                newCmd.CommandType = CommandType.Text;
                newCmd.CommandText = "EXEC dbo.sp_static_alerts_call_mailer '" + rundate + "','" + alertdate + "'";
                newCmd.CommandTimeout = 0;
                newCmd.ExecuteNonQuery();
            //}
            //catch
            //{
            //    MessageBox.Show("Some Stored Procedures Failed, Please check Alert Log!");
            //}
        }
    }
}





我尝试过:



执行存储过程时更新ProgressBar



What I have tried:

Updating a ProgressBar while executing a stored procedures

推荐答案

您收到错误的原因。解决方案是通过委托调用对象。



假设您在表单中有一个Label,并且您希望从另一个Thread更改其Text()属性或者从BackGroundWorker()然后你首先使用Delegate调用主线程。



Dim WithEvents Label1作为新标签()



You are getting the error for reason. The solution is to call the Object via a Delegate.

Assuming you have a Label within the Form and you want its Text() Property to be changed from a different Thread or from a BackGroundWorker() then u first invoke the main thread using a Delegate.

Dim WithEvents Label1 As New Label()

Private Delegate Sub D_ChangeText(ByVal str As String)
  Private Sub ChangeText(ByVal str As String)
      If Me.InvokeRequired Then
          Me.Invoke(New D_ChangeText(AddressOf ChangeText), str)
      Else
         'DO WORK HERE
          Label1.Text = str
      End If
  End Sub





要调用它,请编写ChangeText(Hello World!)



To call it, write ChangeText("Hello World!")


这篇关于c#问题中的进度条和后台工作程序(存储过程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆