如何使用按钮事件

时间:2017-06-04 19:13:59

标签: c# events button timer progress-bar

我是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());
    }

我该如何实现呢?

感谢

1 个答案:

答案 0 :(得分:0)

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

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
        }

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

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;
}

我知道多线程很难首先解决,这里是good tutorial with code samples

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

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

RowSelect = .List(r, 9)