WinForms C# 中的多线程

问题描述 投票:0回答:2

我是新手,我从 StackOverflow 学到了很多东西。我最近开始在我的 Windows 应用程序中使用线程。据我所知,多线程使事情变得简单,就像同时处理很多事件一样。

我有 SQL 存储过程,我用几种方法调用 then。

这是我的代码

private void EditCustomer_Load(object sender, EventArgs e)
        {
            screenszize_Location();

            Thread BackgroundThread = new Thread
                (
                new ThreadStart(() =>
                {
                    GridCustomerList.BeginInvoke(
                     new Action(() =>
                     {
                         LoadGrid();
                     }
                     ));
                }

                ));
            BackgroundThread.Start();


            Thread BackgroundThread1 = new Thread
                (
                new ThreadStart(() =>
                {
                    ComboBxVechicleNumber.BeginInvoke(
                     new Action(() =>
                     {
                         LoadVnum();
                     }
                     ));
                }


                ));
            BackgroundThread1.Start();

            Thread BackgroundThread2 = new Thread
                (
                new ThreadStart(() =>
                {
                    ComboBxBikeMake.BeginInvoke(
                     new Action(() =>
                     {
                         loadBikeMake();
                     }
                     ));
                }


                ));
            BackgroundThread2.Start();

        }

这的作用是, * 屏幕布局 * 加载一个包含 2000 行的 3 列网格 * 将车辆编号从 SQL 表加载到组合框中。 * 将自行车名称从 SQL 表加载到组合框中。

我的电脑速度很快,性能最佳,但我加载的表格仍然冻结并变成白色几秒钟,然后加载。

Please Check the image and you can see the freezer screen

我是否做错了整个线程?

c# sql multithreading winforms stored-procedures
2个回答
2
投票

您没有显示 LoadGrid 方法定义,但我猜它从数据库中获取数据,然后在 DataGridView 或 BindingSource 上设置数据源。

您应该拆分这 2 个步骤,以便仅在 UI 线程上完成数据源的设置,而数据的获取仍然在后台线程中进行。

类似这样的:

Thread BackgroundThread = new Thread
(
new ThreadStart(() =>
{
    //Fetch data here
    GridCustomerList.BeginInvoke(
     new Action(() =>
     {
         //Set DataSource here
     }
     ));
}

));

干杯


0
投票

感谢@mrlucmorin、Drik、TomTom 和 Every1

Hea 再次成为我的代码。

private void LoadGrid()
        {
            Thread BackgroundThread = new Thread
                (
                new ThreadStart(() =>
                {
                    ConnectionStringSettings consetting = ConfigurationManager.ConnectionStrings["AutoDB"];

                         String ConnectionString = consetting.ConnectionString;

                         SqlConnection con = new SqlConnection(ConnectionString);

                             SqlCommand command = new SqlCommand();
                             DataSet ds = new DataSet();
                             SqlDataAdapter da = new SqlDataAdapter();

                             int i = 0;

                             con.Open();
                             command.Connection = con;
                             command.CommandType = CommandType.StoredProcedure;
                             command.CommandText = "LoadAllCustomers";


                             da.SelectCommand = command;
                             da.Fill(ds, "dbo.TblCustomers");

                    GridCustomerList.BeginInvoke(
                     new Action(() =>
                     {

                         GridCustomerList.DataSource = ds.Tables["dbo.TblCustomers"];

                     }
                     ));
                }

                ));
            BackgroundThread.Start();


        }

效果如预期完美:)

© www.soinside.com 2019 - 2024. All rights reserved.