加载圆延迟C#

时间:2015-07-16 16:38:10

标签: c# visual-studio-2010 visual-c#-express-2010

我有一个将数据加载到数据网格的应用程序。但是,加载过程需要几秒钟。我希望在数据加载时显示加载圈,但在数据完成加载之前,圆圈不会显示。代码是:

public void processbutton(object sender, EventArgs e)
        {
     pictureBox1.Visible = true;
     Image image=Image.FromFile("C:\\Users\\me\\Documents\\Curr\\Projects\\Proj\\Proj\\load.gif");
     pictureBox1.Image = image;

//--------------------------------------------------------------------------
     string ssqlconnectionstring = "string";
     SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);

     string comm = "SELECT * from Table";
     sqlconn.Open();

//---------------------REST OF CODE-------------------------

1 个答案:

答案 0 :(得分:0)

您必须从单独的线程执行所有长时间运行的任务,否则会阻止UI线程。

查找BackgroundWorker

干杯

编辑:

或者您可以使用Task类,如下所示:

public async void processbutton(object sender, EventArgs e)
{
    pictureBox1.Visible = true;
    Image image=Image.FromFile("C:\\Users\\me\\Documents\\Curr\\Projects\\Proj\\Proj\\load.gif");
    pictureBox1.Image = image;

    //--------------------------------------------------------------------------
    await Task.Run(() =>
    {
        string ssqlconnectionstring = "string";
        SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);

        string comm = "SELECT * from Table";
        sqlconn.Open();
        //---------------------REST OF CODE-------------------------            
    });