调用异步方法后,C#SqlDependency不触发

时间:2019-04-17 13:48:10

标签: c# sql-server multithreading winforms

我用C#中的SqlDependency编写sql推回WinForm应用程序 一切顺利,但: 当另一种形式的SqlDependency调用异步方法未触发时... 我的主要表格是:表格1: 这是我的代码:

SliceType

我用这段代码谈论RegisterNotification2()方法:

        public Form1()
    {
        InitializeComponent();
        DevExpress.Skins.SkinManager.EnableFormSkins();
        RegisterNotification();
        RegisterNotification2();

    }

和sqlDependency_OnChange2的这些代码:

        private void RegisterNotification2()
    {
        string connectionString = "Data Source=sql.restaurant-mammamia.ir,1430;" +
   "Initial Catalog=1883_mamamiarest;USER ID=1883_mamamiauser;Password=*******#;Trusted_Connection=False; Asynchronous Processing=true"; ;
        string commandText = @"Select [dbo].[tickettbl].[id]  From [dbo].[tickettbl] ";
        SqlDependency.Start(connectionString);

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(commandText, connection))
            {
                command.Notification = null;
                connection.Open();
                var sqlDependency = new SqlDependency(command);
                sqlDependency.OnChange += new OnChangeEventHandler(sqlDependency_OnChange2);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                }
            }
        }
    }

确切的问题:在Form 6中调用异步方法SqlDependency停止工作: 这些是表格6的异步方法的代码:

        private void sqlDependency_OnChange2(object sender, SqlNotificationEventArgs e)
    {
        if (e.Info == SqlNotificationInfo.Insert)
        {
            db d = new db();
            LastTicket lotemp = d.GetLastTicket();
            Invoke(new Action(() =>
            {
                XtraMessageBox.AllowCustomLookAndFeel = true;
                XtraMessageBox.Show("شماره تیکت :"+" "+lotemp.id.ToString()+"\n"+"موضوع تیکت :"+" "+lotemp.subject, "تیکت جدید دریافت شد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Form11 f4 = new Form11();
                if (fc["Form11"] == null)
                {
                    f4.MdiParent = this;
                    f4.simpleButton1.PerformClick();
                    f4.Show();
                }
                else
                {

                    fc["Form11"].Activate();
                    f4.simpleButton1.PerformClick();
                }
            }));
            SqlDependency dependency = sender as SqlDependency;
            dependency.OnChange -= sqlDependency_OnChange2;
            RegisterNotification2();
        }
    }

在调用这些方法之前,一切正常,在异步方法SqlDependency上面的调用之后,一切都很好。 Thx所有编码器。

0 个答案:

没有答案