在win表单中异步加载来自DB的数据

时间:2010-12-18 18:05:49

标签: c#

很多时候我们在表单加载中使用DB中的数据填充UI,这就是为什么表单会冻结几秒钟。所以我只想知道如何异步加载数据并在表单加载中填充UI,因此我的表单不会冻结,也会响应,但我不想使用后台工作者类。请帮我提供可以解决我问题的示例代码。

感谢

4 个答案:

答案 0 :(得分:9)

这是一个评论很好的示例代码:

实施例

// This method can be called on Form_Load, Button_Click, etc.
private void LoadData()
{
    // Start a thread to load data asynchronously.
    Thread loadDataThread = new Thread(LoadDataAsync);
    loadDataThread.Start();
}

// This method is called asynchronously
private void LoadDataAsync()
{
    DataSet ds = new DataSet();

    // ... get data from connection

    // Since this method is executed from another thread than the main thread (AKA UI thread),
    // Any logic that tried to manipulate UI from this thread, must go into BeginInvoke() call.
    // By using BeginInvoke() we state that this code needs to be executed on UI thread.

    // Check if this code is executed on some other thread than UI thread
    if (InvokeRequired) // In this example, this will return `true`.
    {
        BeginInvoke(new Action(() =>
        {
            PopulateUI(ds);
        }));
    }
}

private void PopulateUI(DataSet ds)
{
    // Populate UI Controls with data from DataSet ds.
}

答案 1 :(得分:2)

Command.BeginExecuteReader()

可能满足您阅读的需要。

此方法的Sample Code

您可以在等待响应时致电Application.DoEvents()以保持窗口响应。

答案 2 :(得分:1)

看看这篇文章。 http://aspadvice.com/blogs/azamsharp/archive/2007/04/05/Executing-a-Query-Asynchronously-in-.NET-2.0.aspx

它仍然使用Background worker。老实说,除了将应用程序线程化以执行查询并绑定返回的结果之外,我无法想到替代解决方案。如果你决定使用线程,那么我建议你看看这篇关于异步执行的线程池的文章:http://www.yoda.arachsys.com/csharp/threads/threadpool.shtml

答案 3 :(得分:0)

您最好的做法是使用另一个主题。您可以通过调用ThreadPool.QueueUserWorkItem直接从线程池中使用一个。

    private void OnFormLoad()
    {
        ThreadPool.QueueUserWorkItem(() => GetSqlData());
    }

    private object GetSqlData()
    {
        using (var connection = new SqlCeConnection("ConnectionString"))
        {
            using(var command = new SqlCeCommand())
            {
                command.Connection = connection;
                command.CommandText = "SELECT * FROM tbl_hello";
                command.ExecuteReader();

                while (command.ExecuteReader().Read())
                {
                    //Put data somewhere
                }
            }
        }
    }