如何在5秒后出现一个图片框

时间:2014-04-25 22:33:35

标签: c# winforms

我想知道在5秒后我会使用什么编码来拍照?

例如

PictureBox1.visible = false

然后我想在图片再次可见之前等待5秒

PictureBox1.visible = true

请有人将此代码加入此代码

公共部分类Form1:表单     {

    int horiz, vert, step;


    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick_1(object sender, EventArgs e)
    {


        //image is moved at each interval of the timer

        goblin.Left = goblin.Left + (horiz * step);
        goblin.Top = goblin.Top + (vert * step);


        // if goblin has hit the RHS edge, if so change direction left
        if ((goblin.Left + goblin.Width) >= (Form1.ActiveForm.Width - step))
            horiz = -1;

        // if goblin has hit the LHS edge, if so change direction right
        if (goblin.Left <= step)
            horiz = 1;

        // if goblin has hit the bottom edge, if so change direction upwards
        if ((goblin.Top + goblin.Height) >= (Form1.ActiveForm.Height - step))
            vert = -1;

        // if goblin has hit the top edge, if so change direction downwards
        if (goblin.Top < step)
            vert = 1;
    }

    private void Form1_Load_1(object sender, EventArgs e)
    {
        //Soon as the forms loads activate the goblin to start moving 
        //set the intial direction
        horiz = 1;  //start going right
        vert = 1;   //start going down
        step = 5;
        timer1.Enabled = true;

    }

}

}

4 个答案:

答案 0 :(得分:7)

您可以使用async / await功能轻松完成此操作。

private async void YourFunction()
{
    PictureBox1.Visible = false;
    await Task.Delay(5000);
    PictureBox1.Visible = true;
}

或者,您也可以使用计时器:

private void YourFunction()
{
    Timer tm = new Timer();
    tm.Interval = 5000;
    tm.Tick += timerTick;
    PictureBox1.Visible = false;
    tm.Enabled = true;
    tm.Start();
}

private void timerTick(object sender, EventArgs e)
{
    PictureBox1.Visible = true;
    ((Timer) sender).Stop();
}

答案 1 :(得分:0)

System.Threading.Thread.Sleep(5000);

这应该可以解决问题。

答案 2 :(得分:0)

Private WithEvents tim As New System.Timers.Timer
    Public Delegate Sub doSub()
    Private thingToDo As doSub
    Dim tt = New ToolTip()

    Public Sub TimeOut(d As doSub, milliseconds As Integer)
        thingToDo = d
        tim.AutoReset = False
        tim.Interval = milliseconds
        tim.Start()
    End Sub

    Private Sub tim_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles tim.Elapsed
        Invoke(thingToDo)

    End Sub


    Private Sub hide()
        picturebox.hide()

    End Sub
    Private Sub show()
        TimeOut(Address of hide, 5000)

    End Sub

到C# http://www.developerfusion.com/tools/convert/csharp-to-vb/

答案 3 :(得分:0)

使用后台工作程序,因此您将在后台“冻结”线程,这样它将使您的应用程序响应。

看看这里:

 Private Sub Execute()
    Dim BGW As New System.ComponentModel.BackgroundWorker
    AddHandler BGW.DoWork, AddressOf WorkerDoWork
    AddHandler BGW.ProgressChanged, AddressOf WorkerProgressChanged
    AddHandler BGW.RunWorkerCompleted, AddressOf WorkerCompleted

    With BGW
        .WorkerReportsProgress = True
        .WorkerSupportsCancellation = True
        .RunWorkerAsync()
    End With
End Sub
Private Sub WorkerDoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
    ' Do some work
    Threading.Thread.Sleep(5000)
End Sub

Private Sub WorkerProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs)
    ' I did something!
End Sub

Private Sub WorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs)
    ' Show your picture here 
    Picturebox1.Visible = True
End Sub

这是你的所有应用程序都不会冻结,你的gui也会响应。请看一下ThreadPools或.net中的任务