如何打开一个表单,然后在c#上的X秒后关闭它?

时间:2016-08-24 22:43:22

标签: c# forms time timer

我需要在C#上打开一个表单然后在5秒之后自动关闭..事情是,我需要从打开的表单中关闭表单,因为我打开的表单是有时从另一种形式打开而不会自动关闭。

我试过这个:

        private void button6_Click(object sender, EventArgs e)
    {
        GetNumber gtn = new GetNumber();
        gtn.Show();
        System.Threading.Thread.Sleep(6000);
        gtn.Hide();
        gtn = null;
    }

但是当它开始时表格搞砸了,当我尝试使用计时器时也是如此。 有谁知道如何解决我的问题?

2 个答案:

答案 0 :(得分:2)

正如它所说的那样,计时器会为你想做的事情而努力。如果您使用的是.Net 4.5或更高版本,则可以使用async / await功能。

在核心,你需要释放UI线程继续前进。你的Thread.Sleep使UI线程无法使用。使用计时器或async / await允许UI线程启动对话框并处理任何用户操作。

private async void button6_Click(object sender, EventArgs e)
{
    GetNumber gtn = new GetNumber();
    gtn.Show();
    await Task.Delay(6000);
    gtn.Hide();
    gtn = null;
}

答案 1 :(得分:0)

计时器将是正确的方法:

    <?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = 'Jonathan@slickpixeldesigns.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>