【问题标题】:how to make progress bar work with functions in button event如何使进度条与按钮事件中的功能一起使用
【发布时间】:2017-11-05 13:44:59
【问题描述】:

我是 C# 新手,所以请耐心等待。

我想让进度条与我在程序中创建的任何功能一起工作

我有课来检查 INTERNET 是否可用以及数据库状态的连接

我有 "progressBar1" ,样式是 "Marquee"

我只是想表明程序中有一个流程工作“功能”,我不需要有步骤或计时器来增加它

只需让进度工作,直到函数完成其代码并且函数将在按钮事件中工作(当我按下按钮时

这是我的代码

class checkInternet
{
    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

public bool checkInternetAvailable()
{
    int Desc;
    bool result = false;

    if (InternetGetConnectedState(out Desc, 0) == true)
    {
        try
        {
            dbConnection StartConn = new dbConnection();
            SqlConnection MyConnetion = StartConn.GetConnection();

            MyConnetion.Open();

            if (MyConnetion.State == ConnectionState.Open)
            {
                result = true;
            }

            MyConnetion.Close();
        }
        catch (Exception)
        {
            result = false;
            MessageBox.Show("The database connection does not available, May be because of this reasons: \n\n1- there is a new version of the program avalible. \n2- database has some maintenance. \n\n Please check later :)", "Conection status");
        }
    }
    else
    {
        result = false;
        MessageBox.Show("No internet connection avalible , Please check later :) \nThanks.", "Conection status");
    }

    return result;
}
}

这就是我在按钮事件中所拥有的

    private void button1_Click(object sender, EventArgs e)
    {
        checkInternet check = new checkInternet();

        progressBar1.Value = 0;
        do
        {
            progressBar1.PerformStep();

        } while (check.checkInternetAvailable());
    }

我该如何实现?

谢谢

【问题讨论】:

  • 你可以尝试使用异步等待
  • 我怎样才能做到我会尝试任何事情来做到这一点,但如何?

标签: c# events button timer progress-bar


【解决方案1】:

据我了解,您希望用户在检查连接任务在后台执行时看到进度条。 checkInternetAvailable 将是您的后台操作,我不建议直接显示消息。而是返回一个自定义结构:

   public struct ConnectionCheckResult
   {
       public bool Success;
       public string Message;
   }

这将是您的按钮单击事件处理程序:

private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Style = ProgressBarStyle.Marquee;
            progressBar1.Visible = true;
            //add code here to be executed on UI thread before connection check
            Task.Run(new Action(() => 
            {
                //Task.Run this code on the thread pool instead of your UI thread. So your code is checking connection while progress bar is still rendering
                ConnectionCheckResult res = new checkInternet().checkInternetAvailable();
                this.Invoke(new Action(() => 
                {
                    //this.Invoke executes following delegate on UI thread. All UI changes - like progressBar1.Visible = false; need to be made in UI thread.
                    //add code here to be executed on the UI thread after connection check.
                    progressBar1.Visible = false;
                    if (!string.IsNullOrEmpty(res.Message))
                        MessageBox.Show(res.Message);

                }));

            }));
            //add code to be executed on UI thread at the same time as connection check
        }

我知道一开始你很难理解多线程,这里是good tutorial with code samples

此外,当您的进度条样式为 Marquee 时,您无需调用 PerformStep。它会自己滚动。

编辑:您还应该像这样修改 checkInternetAvailable() :

public ConnectionCheckResult checkInternetAvailable()
{
    int Desc;
    ConnectionCheckResult result = new ConnectionCheckResult();

    if (InternetGetConnectedState(out Desc, 0) == true)
    {
        try
        {
            dbConnection StartConn = new dbConnection();
            SqlConnection MyConnetion = StartConn.GetConnection();

            MyConnetion.Open();

            if (MyConnetion.State == ConnectionState.Open)
            {
                result.Success = true;
            }

            MyConnetion.Close();
        }
        catch (Exception)
        {
            result.Success = false;
            result.Message = "The database connection does not available, May be because of this reasons: \n\n1- there is a new version of the program available. \n2- database has some maintenance. \n\n Please check later :)";
        }
    }
    else
    {
        result.Success = false;
        result.Message = "No internet connection available , Please check later :) \nThanks.";
    }

    return result;
}

【讨论】:

  • ` checkInternet check = new checkInternet(); progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.Visible = true; Task.Run(new Action(() => { check.checkInternetAvailable(); this.Invoke(new Action(() => { progressBar1.Visible = false; //MessageBox.Show("Finsh", "test") ; })); }));`
  • 不能隐式地将类型 'bool' 转换为 'checkInernetAndDatabase.Form1.ConnectionCheckResult' checkInernetAndDatabase 这就是使用您的代码得到的结果,但是如果您看到它,我会对其进行小幅编辑第一次会很好,但第二次会很快,但我认为这很好
  • 我在答案中添加了修改后的 checkInternetAvailable()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多