Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
windows中的多线程fromsc#_C#_Sql_Multithreading_Winforms_Stored Procedures - Fatal编程技术网

windows中的多线程fromsc#

windows中的多线程fromsc#,c#,sql,multithreading,winforms,stored-procedures,C#,Sql,Multithreading,Winforms,Stored Procedures,我是新手,从StackOverflow中学到了很多东西。我最近开始在我的windows应用程序中使用线程。据我所知,多线程使事情变得简单,比如同时做很多事情 我有SQL中的存储过程,然后用几个方法调用 这是我的密码 private void EditCustomer_Load(object sender, EventArgs e) { screenszize_Location(); Thread BackgroundThread

我是新手,从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表加载到组合框中

我的计算机速度很快,性能最好,但我加载的表单仍然会冻结,并在几秒钟内变为白色,然后加载


我的整个线程设计都错了吗?

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

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

大概是这样的:

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

));

干杯

感谢@Lucmorin先生、Drik、TomTom和Every1

他又是我的代码

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


        }

它可以像预期的那样完美地工作:)

您启动新线程只是为了通过
BeginInvoke
将执行传递给UI线程,您对发布的代码所做的是在UI线程中创建一个线程,将工作委托回UI线程。您不妨使用
EditCustomer\u Load
方法来完成这项工作。您可能想做的是在一个单独的线程中从数据库加载数据,然后在UI线程上调用一个方法来显示数据。@Drik“实际上,
EditCustomer\u Load
是FormLoad方法,
LoadGrid
方法在UI线程上被调用。在再次调用
GridCustomerList.BeginInvoke
之前,您应该完成耗时较长但不需要访问UI元素的工作。读他说的话。他说,在线程中加载数据,然后调用回UI,将数据放入控件中。在现代的C语言中(不是完全过时的),在这里你最好使用异步而不是线程。谢谢你说得很清楚:)我会马上试试。及其工作原理:)