6

many time we populate UI with data from DB in the form load and that is why form gets freeze for few second. so i just want to know how can i load data asynchronously and populate UI in form load as a result my form will not freeze and also will be responsive but i don't want to use background worker class. please help me with sample code which can solve my problem.

thanks

1
  • 4
    Why don't you want to use a background worker? Commented Dec 18, 2010 at 18:07

4 Answers 4

12

Here is a well commented example code:

Example:

// 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.
}
Sign up to request clarification or add additional context in comments.

Comments

2
Command.BeginExecuteReader()

may serves your need for reading purposes.

Here is Sample Code for this method.

You can call Application.DoEvents() while waiting for response to keep your window responsive.

2 Comments

+1 : good idea. Also @Downvoter, please be courteous enough to let everyone know why you felt the need to downvote this one.
I know it's old, but here's the reason for the downvote: blog.codinghorror.com/is-doevents-evil
1

Have a look at this article. http://aspadvice.com/blogs/azamsharp/archive/2007/04/05/Executing-a-Query-Asynchronously-in-.NET-2.0.aspx

It still uses Background worker. Honestly, I can't think of an alternative solution to this this other than threading your application to execute queries and bind returned results. If you do decide to use threads, than i suggest you take a look at this article about thread-pooling for asynchronous execution: http://www.yoda.arachsys.com/csharp/threads/threadpool.shtml

Comments

0

Your best course of action is to use another thread. You can use one straight from the thread pool by calling 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
                }
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.