【问题标题】:C# gif not loading -C# gif 未加载 -
【发布时间】:2017-03-21 17:09:28
【问题描述】:

我有一个表单,它在单击按钮时从DataGridView 运行查询。下面的代码按我的预期工作;

  1. 打开一个显示“查询运行时请稍候”的表单
  2. 运行查询
  3. 加载时关闭表单,然后用 数据。

我添加了一个带有简单 gif 的图片框 - 纯粹是为了让 UI 和用户看到它正在工作。但 gif 实际上并没有旋转 - 只是显示为图片。测试它,如果我自己在表单上运行它就可以了,我只能冒险猜测正在运行的查询正在阻止它按应有的方式工作。

        PleaseWaitForm pleaseWait = new PleaseWaitForm();
        try
        {
            pleaseWait.Show();
            Application.DoEvents();

            this.singleCenTableAdapter.Fill(this.singleCenData.SingleCenTable, ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
            int RowC = singleCenTableDataGridView.RowCount;
            pleaseWait.Close();
            if (RowC == 0)
            {
                MessageBox.Show(GlobVar.NoResults, "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            pleaseWait.Close();
        }
        catch (System.Exception exc)
        {
            GlobVar.vid.Open();
            var LogName = GlobVar.LoginName.ExecuteScalar();
            GlobVar.vid.Close();
            MessageBox.Show
                (
                "An error has occured " + LogNam + ". Please try again. \n\n" +
                exc.Message, "An error has occured", MessageBoxButtons.OK, MessageBoxIcon.Warning
                );
        }
        finally
        {
            pleaseWait.Close();
        }

“请稍候”表单只是一个标签和图片,所以此时只有 Initalize ;

    public PleaseWaitForm()
    {
        InitializeComponent();
    }

是否有人对如何解决此问题以使其正常工作有任何想法?或者我做错了什么特别的事情?我知道在大多数情况下我可能会因为使用Application.DoEvents() 而得到一些支持,但我们不胜感激!

【问题讨论】:

标签: c#


【解决方案1】:

我在your comment 中读到您使用.NET 4.0,因此不能将awaitasyncTask<> 一起使用。

.NET 4.5之前,可以使用Thread或者BackgroundWorker来实现,
参考下面BackgroundWorker的例子:

Background Worker loading screen in Winforms

您当前的代码正在同步运行,您希望它能够异步运行。

【讨论】:

    【解决方案2】:

    您需要 asyncawait 来保持您的 UI 响应。

    首先将这段代码在async中的方法如下:

    private async void Method()
    {
        ...
    }
    

    接下来将以下代码放入新方法中:

    this.singleCenTableAdapter.Fill(this.singleCenData.SingleCenTable, ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
    int RowC = singleCenTableDataGridView.RowCount;
    

    例如:

    private async Task<int> FillAndGetRowCount( ... ) // add parameters if needed
    {
        this.singleCenTableAdapter.Fill(this.singleCenData.SingleCenTable, ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
        return singleCenTableDataGridView.RowCount;
    }
    

    最后,将 try 块更改为:

    try
    {
        pleaseWait.Show();
        Application.DoEvents();
    
        int RowC = await FillAndGetRowCount( ... );
        pleaseWait.Close();
        if (RowC == 0)
        {
            MessageBox.Show(GlobVar.NoResults, "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
    }
    

    【讨论】:

    • 你好 - 很抱歉迟到了一段时间,我一直在尝试这样做,我只是得到了很多不同的错误。每次都是这个; “Task”不包含“GetAwaiter”的定义,并且找不到接受“Task”类型的第一个参数的扩展方法“GetAwaiter”(您是否缺少 using 指令或程序集引用?)
    • @Zoltan 您使用的是 .NET 4.5 或更高版本吗?或者您使用的是旧版本的 .NET?我的解决方案仅适用于 .NET 4.5 及更高版本。
    • 啊!!!我将在 .NET 4.5 或更高版本上创建一个测试项目并进行测试。我不得不将框架降低到 .NET 4.0,因为用户只安装了这个并且 IT 没有足够的权限让他们安装 4.5 或更高版本
    • @Zoltan 也许使用Microsoft.Bcl.Async NuGet 包也是您的一种选择?另请参阅以下内容:stackoverflow.com/a/39662411/2541501
    • @Zoltan 如果您必须使用.NET 4.0,您也可以决定使用ThreadBackgroundWorker,正如@Rajendra 在his answer 中所建议的那样,因为这是进行这种排序的方式.NET 4.5 之前的事情。
    猜你喜欢
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2022-12-12
    • 2021-04-06
    • 2010-12-27
    • 2014-07-26
    • 1970-01-01
    相关资源
    最近更新 更多