我在使用 Thread.sleep(seconds)时遇到问题,它会暂停我在睡眠状态下的所有执行。但是我尝试了另一种解决方案,也使用 for loop,但是我期望它不起作用。
单击登录按钮时:
- 行动报告="关于进展";
- 再过 2 秒,它将“尝试连接到数据库”
- 然后打开数据库后会像“数据库连接成功”
这是代码:
private void Loginbtn_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if(userText.Text!=String.Empty && passText.Password!=String.Empty){
ProgressForm.Visibility = System.Windows.Visibility.Visible;
LoginForm.Visibility = System.Windows.Visibility.Hidden;
delay(2);
actionReport.Text = "Try to Connecting the database";
String ConnectionString = "server=127.0.0.1;uid=root;pwd='';database=smsdb;";
MySqlConnection con = new MySqlConnection(ConnectionString);
try {
con.Open();
delay(2);
actionReport.Text = "Database Connected Sucessfully";
}
catch(MySqlException sqle){
actionReport.Text = sqle.Message;
}
}
else {
MessageBox.Show("Please enter the user name and password to verify","Notification",MessageBoxButton.OK,MessageBoxImage.Information);
}
}
private void delay(int seconds)
{
for(long i=0;i<seconds*3600; i++){
//empty
}
请有人帮助我。