Free UI thread when long method works using await and async

I'm starting to research async and wait for keywords from C # 5.0, so I did some tests using winforms, but now I'm stuck in a situation:

My task is to start the request from the database, and until the request is complete, I want to show the gif for upload in the form.

This is the method that queries the database:

public static async Task<List<string>> GetItensFromDatabase()
    {
        List<string> names = new List<string>();

        using (ServerConn)
        {
            ServerConn.Open();
            SqlCommand cmd = new SqlCommand("Select * From Names", ServerConn);

            var ds = new DataSet();
            var adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds); // this call lasts about 1 minute

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                names.Add(dr["Name"].ToString());
            }
        }
        return names;
    }

And I call this method here:

private async void button1_Click(object sender, EventArgs e)
    {
        List<string> itens = null;
        itens = await AsyncMethods.GetItensFromDatabase(); // I want that the form dont be stuck here

        ShowItensInListView(itens);
    }

Now I have a ubiquitous downloadable gif image on a form that rotates until the GetItensFromDatabase method is called, when the method is launched, the gif stops and when the method finishes running the gif again.

So, is there a way to keep the gif rotating when the GetItensFromDatabase method is run?

+4
1

await async, , Task , TaskFactory:

public static Task<List<string>> GetItensFromDatabase()
{
    return Task.Factory.StartNew<List<string>>(() => 
    { 
        List<string> names = new List<string>();

        using (ServerConn)
        {
            ServerConn.Open();
            SqlCommand cmd = new SqlCommand("Select * From Names", ServerConn);

            var ds = new DataSet();
            var adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds); // this call lasts about 1 minute

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                names.Add(dr["Name"].ToString());
            }
        }
        return names;
    });
}

: @mmarques, () SqlDataReader SqlDataAdapter. , SqlDataReader async - , TaskFactory , async await.

public static async Task<List<string>> GetItensFromDatabase()
{
    List<string> names = new List<string>();

    using (ServerConn)
    {
        using (SqlCommand cmd = new SqlCommand("Select * From Names", ServerConn))
        {
            ServerConn.Open();

            SqlDataReader reader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection);
            if (reader.HasRows)
            {
                DataTable dt = new DataTable();
                dt.Load(reader);

                foreach (DataRow dr in dt.Rows)
                {
                    names.Add(dr["Name"].ToString());
                }
            }
        }
    }

    return names;
}
+2

All Articles