C # SqlDependency не запускается после вызова асинхронного метода - PullRequest
0 голосов
/ 17 апреля 2019

Я пишу WinForm Application, использую sql push обратно с SqlDependency в C # Все хорошо и работает, но: Когда вызывается асинхронный метод в другой форме, Sqldependency не запускается ... Моя основная форма: Форма 1: это мой код:

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

    }

Я говорю о методе RegisterNotification2 () с этим кодом:

        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())
                {
                }
            }
        }
    }

и этот код sqlDependency_OnChange2:

        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();
        }
    }

Собственно проблема: когда в форме 6 вызывается асинхронный метод SqlDependency Остановка работы: Это мой код метода асинхронной формы 6:

        private async void repositoryItemButtonEdit1_Click(object sender, EventArgs e)
    {
        XtraMessageBox.AllowCustomLookAndFeel = true;
        DialogResult dr= XtraMessageBox.Show("آیا از تایید این سفارش ممئن هستید ؟"+"\n"+"با این کار کاربر در آپ ناتیفیکیشن تایید دریافت می کند", "آیا مطمئن هستید ؟", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if(dr==DialogResult.Yes)
        {
            progressPanel1.Visible = true;
            int id = Int32.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "id").ToString());
            string pid = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "pusheid").ToString();
            int iu=await sendnotification(id, pid, "تایید");
            await wbs.UpdateOrderTblAsync(id, "تایید شد");
            XtraMessageBox.AllowCustomLookAndFeel = true;
            XtraMessageBox.Show("سفارش تایید و از طریق ناتیفیکیشن به کاربر اطلاع رسانی شد", "موفقیت", MessageBoxButtons.OK, MessageBoxIcon.Information);
            sqlDataSource1.Fill();
            progressPanel1.Visible = false;
        }
    }

Перед вызовом этих методов Все в порядке и все в порядке Точно после вызова выше Асинхронный метод SqlDependency не работает. Thx All Coder.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...