使用ExecuteNonQueryAsync和报告进度 [英] Using ExecuteNonQueryAsync and Reporting Progress

查看:60
本文介绍了使用ExecuteNonQueryAsync和报告进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我正在尝试做一些非常简单的事情.我只想在屏幕上报告一个运行号,以便用户了解我正在执行的SQL存储过程正在工作,并且他们不会感到不耐烦,因此开始单击按钮.

I thought I was trying to do something very simple. I just want to report a running number on the screen so the user gets the idea that the SQL Stored Procedure that I'm executing is working and that they don't get impatient and start clicking buttons.

问题是我无法弄清楚如何为ExecutNonQueryAsync命令实际调用进度报告程序.它卡在我的报告循环中,并且从不执​​行该命令,但是,如果我将其放在async命令之后,它将得到执行,并且结果永远不会等于零.

The problem is that I can't figure out how to actually call the progress reporter for the ExecutNonQueryAsync command. It gets stuck in my reporting loop and never executes the command but, if I put it after the async command, it will get executed and result will never not equal zero.

任何想法,评论和想法将不胜感激.非常感谢!

Any thoughts, comments, ideas would be appreciated. Thank you so much!

        int i = 0;
        lblProcessing.Text = "Transactions " + i.ToString();
        int result = 0;
        while (result==0)
        {
            i++;
            if (i % 500 == 0)
            {
                lblProcessing.Text = "Transactions " + i.ToString();
                lblProcessing.Refresh();
            }

        }
        //  Yes - I know - the code never gets here - that is the problem! 
        result = await cmd.ExecuteNonQueryAsync();

推荐答案

您是否只想让用户知道正在发生的事情,并且实际上不需要显示当前进度?

Do you just want to let the user know that something is happening, and you don't actually need to display current progress?

如果是这样,您只需显示一个 ProgressBar ,其 Style 设置为 Marquee .

If so, you could just display a ProgressBar with its Style set to Marquee.

如果您希望此方法是独立的"方法,则可以在模式窗体上显示进度条,并将窗体代码包含在方法本身中.

If you want this to be a "self-contained" method, you could display the progress bar on a modal form, and include the form code in the method itself.

例如

public void ExecuteNonQueryWithProgress(SqlCommand cmd) {
    Form f = new Form() {
        Text = "Please wait...",
        Size = new Size(400, 100),
        StartPosition = FormStartPosition.CenterScreen,
        FormBorderStyle = FormBorderStyle.FixedDialog,
        MaximizeBox = false,
        ControlBox = false
    };
    f.Controls.Add(new ProgressBar() { 
        Style = ProgressBarStyle.Marquee,
        Dock = DockStyle.Fill
    });
    f.Shown += async (sender, e) => {
        await cmd.ExecuteNonQueryAsync();
        f.Close();
    };
    f.ShowDialog();
}

这篇关于使用ExecuteNonQueryAsync和报告进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆