【发布时间】: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