在C#Winforms中缓慢的UI渲染

时间:2016-09-22 09:40:00

标签: c# .net multithreading winforms

我用一些按钮制作了一个相对简陋的WinForms用户控件,每个按钮代表一个数据库中的表。

在调用DataGridView方法后,每个表中的数据都会加载到相应的InitializeComponent中。

当单击上述按钮之一时,其对应的DataGridView和表示通过FK链接到主要按钮的表格变为可见,然后BackgroundWorker逐渐将它们增加到所需的大小和班次在短时间内控制到适当的位置。

public uc1()
{
    InitializeComponent();

    PopulateTables();

    tableResizer.WorkerReportsProgress = true;
    tableResizer.WorkerSupportsCancellation = true;
    tableResizer.DoWork += tableResizer_DoWork;
    tableResizer.ProgressChanged += tableResizer_ProgressChanged;
    tableResizer.RunWorkerCompleted += tableResizer_RunWorkerCompleted;

    dgv5.SelectionChanged += show5Relations;
}

private void btn5_Click(object sender, EventArgs e)
{
    mainTable = 5;
    btn5.Visible = false;
    btn4.Visible = false;
    btn3.Visible = false;
    btn2.Visible = false;
    dgv5.Visible = true;
    dgv4.Visible = true;
    dgv3.Visible = true;
    dgv2.Visible = true;
    lbl6.Visible = false;
    lbl7.Visible = false;
    lbl1.Visible = false;
    tableResizer.RunWorkerAsync();
}

private void tableResizer_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    int expansion = 0;
    while (expansion < 30)
    {
        tableResizer.ReportProgress(mainTable);
        expansion++;
        System.Threading.Thread.Sleep(16);
    }
}

private void tableResizer_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
    if (e.ProgressPercentage == 5)
    {
        dgv5.Size = new Size(dgv5.Size.Width + (((this.Width / 26) * 10) / 30), dgv5.Size.Height + (((this.Height / 24) * 10) / 30));
        dgv5.Location = new Point(dgv5.Location.X - (((this.Width / 26) * 10) / 60), dgv5.Location.Y - (((this.Height / 24) * 10) / 60));

        dgv4.Size = new Size(dgv4.Size.Width + (((this.Width / 26) * 10) / 30), dgv4.Size.Height + (((this.Height / 24) * 10) / 50));
        dgv4.Location = new Point(dgv4.Location.X - (((this.Width / 26) * 10) / 40), dgv4.Location.Y - (((this.Height / 24) * 10) / 120));

        dgv3.Size = new Size(dgv3.Size.Width + (((this.Width / 26) * 10) / 30), dgv3.Size.Height + (((this.Height / 24) * 10) / 50));
        dgv3.Location = new Point(dgv3.Location.X - (((this.Width / 26) * 10) / 40), dgv3.Location.Y - (((this.Height / 24) * 10) / 120));

        dgv2.Size = new Size(dgv2.Size.Width + (((this.Width / 26) * 10) / 30), dgv2.Size.Height + (((this.Height / 24) * 10) / 50));
        dgv2.Location = new Point(dgv2.Location.X - (((this.Width / 26) * 10) / 40), dgv2.Location.Y - (((this.Height / 24) * 10) / 120));

        btn1.Location = new Point(btn1.Location.X - (((this.Width / 26) * 10) / 130), btn1.Location.Y);
    }
}

private void tableResizer_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
    if (mainTable == 5)
    {
        btn7.Visible = true;
        btn6.Visible = true;
        btn1.Visible = true;
        lbl5.Visible = true;
        lbl5.Location = new Point(dgv5.Location.X, dgv5.Location.Y - lbl5.Size.Height);
        lbl4.Visible = true;
        lbl4.Location = new Point(dgv4.Location.X, dgv4.Location.Y - lbl4.Size.Height);
        lbl3.Visible = true;
        lbl3.Location = new Point(dgv3.Location.X, dgv3.Location.Y - lbl3.Size.Height);
        lbl2.Visible = true;
        lbl2.Location = new Point(dgv2.Location.X, dgv2.Location.Y - lbl1.Size.Height);
    }
}

问题是当tableResizer正在执行其工作时,UI的渲染速度不够快,因此,DataGridViews之一在工作完成之前不会完全可见。

DoWork方法中增加Sleep的长度仅在60ms时解决了这个问题,此时渲染太不稳定而无法接受。任何超过30毫秒的东西都不行。

我对多线程相对较新,可以说是C#,所以我很确定有更有效的方法来解决这个问题。

我应该怎么做以确保平滑渲染?

1 个答案:

答案 0 :(得分:1)

我发现了解决问题的方法。虽然之前我曾尝试将DoubleBuffered设置为true,但它没有帮助。问题是DataGridViews默认关闭它,因为它是受保护的属性,所以不能设置为true。

在对这个问题进行进一步调查后(我应该早点完成,我的道歉),我发现了这个问题: Horrible redraw performance of the DataGridView on one of my two screens

解决方案非常简单。创建一个继承自默认DataGridView的类,并将其DoubleBuffered属性指定为true。

class CustomDataGridView: DataGridView
{
    public CustomDataGridView()
    {
        DoubleBuffered = true;
    } 
}