【问题标题】:MultiThreading in windows froms C#Windows窗体中的多线程C#
【发布时间】:2014-04-02 20:47:30
【问题描述】:

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

我在 SQL 中有存储过程,然后我用几种方法调用。

这是我的代码

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 表中的自行车名称加载到组合框中。

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

我做错了整个线程吗?

【问题讨论】:

  • 你启动新线程只是为了通过BeginInvoke将执行传递给UI线程
  • 您对发布的代码所做的事情是在您的 UI 线程中创建一个线程,将工作委托回 UI 线程。您不妨在EditCustomer_Load 方法中完成这项工作。您可能想要做的是在单独的线程中从数据库加载数据,然后调用 UI 线程上的方法来显示数据。
  • @Drik "其实EditCustomer_Load就是FormLoad方法
  • 在 UI 线程上调用 LoadGrid 方法。您应该在调用GridCustomerList.BeginInvoke之前完成耗时但不需要访问UI元素的工作。
  • 再次。读他说的话。他说在线程中加载数据,然后调用回 UI 以将数据放入控件中。虽然在现代 C' 中(并非完全过时),但您最好使用异步而不是线程。

标签: c# sql multithreading winforms stored-procedures


【解决方案1】:

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

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

类似这样的:

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

));

干杯

【讨论】:

  • 感谢您的说明 :) 我会立即尝试。及其工作:)
【解决方案2】:

感谢@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();


        }

按预期完美运行 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2013-06-19
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多