当从SQL加载数据到多个listview时,在c#中显示动画gif

本文关键字:显示 gif 动画 listview 加载 SQL 数据 当从 | 更新日期: 2023-09-27 18:02:30

我创建了一个多人在发货前扫描产品二维码的应用程序。

应用程序是使用WinForms, c#语言和SQL数据库创建的。

我正在使用MDI表单与多个表单,如扫描产品,生成MIS,仪表板等。

应用程序运行得很好,现在我正在进行优化。

我被卡在仪表板表单中,其中包含来自数据库的多个枢轴计数,如每个用户扫描,每个产品扫描,每小时扫描等。

表单工作得很好,但加载这么多数据需要一些时间,我想显示一个动画gif,直到进程执行和列表视图和标签更新。

使用下面的代码图像是不可见的,当代码在后台运行。请指导我该怎么做。

我只粘贴了一个方法,例如其他方法类似:

  private void RefreshBtn_Click(object sender, EventArgs e)
    {
         Cursor.Current = Cursors.WaitCursor;
         pictureBox2.Visible = true;
         displayStats();
         Cursor.Current = Cursors.Arrow;
         pictureBox2.Visible = false;
    }
private void displayStats()
    {
    CustScan();
    DatewiseQcCount();
    UserWiseScan();
    UserwiseDScan();
    DayWiseDispatchScan();
    HourwiseQcCount();
    }
private void UserWiseScan()
    {
        string queryString = "select scanby,count(distinct refno),count(1) from SecRec where scan='Y' group by scanby order by 3,2 desc";
        lstVUser.Clear();
        lstVUser.Columns.Add("User", 105);
        lstVUser.Columns.Add("Cust", 60);
        lstVUser.Columns.Add("Imp", 60);
        using (SqlConnection conn = new SqlConnection(connectString))
        {
            SqlCommand cmd = new SqlCommand(queryString, conn);
            try
            {
                conn.Open();
                SqlDataAdapter adp = new SqlDataAdapter(queryString, conn);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    ListViewItem listitem = new ListViewItem(dr[0].ToString());
                    listitem.SubItems.Add(dr[1].ToString().PadLeft(3));
                    listitem.SubItems.Add(dr[2].ToString().PadLeft(3));
                    lstVUser.Items.Add(listitem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

前端没有太多活动,因为返回行是有限的。大部分流程是在后端完成的。

我想用GIF动画显示pictureBox

当从SQL加载数据到多个listview时,在c#中显示动画gif

添加一个BackgroundWorker worker,将DoWork处理程序分配给你的displayStats,方法,并设置事件。结果为您想要显示的内容,然后在RunWorkerCompleted事件上分配一个方法来使用event.result

中的数据更新UI。

希望有帮助:

Backgorund工人:http://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners

http://www.codeproject.com/Articles/228869/BackgroundWorker-and-UI-threads

您需要在这里创建一个正在加载的表单。您需要创建该形式的对象,并使用线程概念调用FrmLoading放置一个Picturebox,并在其中设置.gif图像。

FrmLoading f2 = new FrmLoading();
using (new PleaseWait(this.Location, () =>MethodWithParameter())) {  f2.Show(this); }
f2.Close();
如上面代码所示,您需要创建PleaseWait类。

PleaseWait.cs

public class PleaseWait : IDisposable
{
    private FrmLoading mSplash;
    private Point mLocation;
    public PleaseWait(Point location, System.Action methodWithParameters)
    {
        mLocation = location;
        Thread t = new Thread(new ThreadStart(workerThread));
        t.IsBackground = true;
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        methodWithParameters();
    }
    public void Dispose()
    {
        mSplash.Invoke(new MethodInvoker(stopThread));
    }
    private void stopThread()
    {
        mSplash.Close();
    }
    private void workerThread()
    {
        mSplash = new FrmLoading();   // Substitute this with your own
        mSplash.StartPosition = FormStartPosition.CenterScreen;
        //mSplash.Location = mLocation;
        mSplash.TopMost = true;
        Application.Run(mSplash);
    }
}