后台工作人员实施 [英] Background Worker implementation

查看:91
本文介绍了后台工作人员实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的后台工作DoWor函数,考虑GUI操作的实现可以吗?

This is my background work DoWor function, is the implementation taking into consideration the GUI operation done okay?

       private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
         try
        {

            string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            string query = "SELECT * FROM dbo.Liguanea_Lane2";
            SqlCommand cmd = new SqlCommand(query, con);

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string scode = dr.GetString(dr.GetOrdinal("code"));

                comboBox2.Invoke((MethodInvoker)delegate
                {
                    comboBox2.Items.Add(scode);
                });
                }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }
    }

这是调用它的函数:

  private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
    {
      if(comboBox4.SelectedIndex == 0)
        {
            backgroundWorker1.RunWorkerAsync();

        }
        else
        {

            MessageBox.Show("This table doesn't exist within the database");
        }
    }

当前什么也没发生.代码只以屏幕上的默认格式运行.没有加载值,我在做什么错了?

Currently nothing happens..the code just runs with the default form on the screen. Not loading the values, what am I doing wrong?

推荐答案

BGW已过时. TPL,async/awaitIProgress<T>可以解决使用BGW的所有情况以及更多情况.

BGW is obsolete. The TPL, async/awaitand IProgress<T> can address all cases where BGW was used and more.

在这种情况下,BGW还是不合适的.真正的延迟是由数据库调用而不是加载UI引起的.要异步执行数据库调用,只需使用异步事件处理程序即可:

In this case, BGW isn't appropriate anyway. The real delay is caused by the database call, not loading the UI. To execute the database call asynchronously, you only have to use an asynchronous event handler:

private async void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
        var conString=Properties.Settings.Default.MyConnectionString;
        string query = "SELECT * FROM dbo.Liguanea_Lane2";                    

        using(var con = new SqlConnection(conString))
        using(var cmd = new SqlCommand(query, con))
        {
            await con.OpenAsync();
            var reader=await cmd.ExecuteReader();
            while (dr.Read())
            {
                string scode = dr.GetString(dr.GetOrdinal("code"));
                comboBox2.Items.Add(scode);
            }
        }
}

虽然有更好的方法来加载组合.一种改进是加载列表中的所有项目,然后使用AddRange更新组合. AddRange已经调用BeginUpdate来阻止UI更新,同时添加列表中的项目.

There are better ways to load a combo though. One improvement is to load all items in a list, then use AddRange to update the combo. AddRange already calls BeginUpdate to prevent UI updates while adding items from the list.

        var items=new List<string>();
        using(var con = new SqlConnection(conString))
        using(var cmd = new SqlCommand(query, con))
        {
            await con.OpenAsync();
            var reader=await cmd.ExecuteReader();
            while (dr.Read())
            {
                string scode = dr.GetString(dr.GetOrdinal("code"));
                list.Add(scode);
            }
        }

        comboBox2.Items.AddRange(items);

更好的是,使用像Dapper这样的微型ORM来消除所有这些代码:

Even better, use a micro-ORM like Dapper to get rid of all this code:

using(var con = new SqlConnection(conString))
{
    await con.OpenAsync();
    var items=await con.QueryAsync<string>(query);
    comboBox2.Items.AddRange(items);
}

这篇关于后台工作人员实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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