module1.vb

来自「适合VB初学者看」· VB 代码 · 共 37 行

VB
37
字号
Imports System
Imports System.Threading
Module Module1
    Sub Main()
        Dim classObject As Class1
        classObject = New Class1()
        '输出当前时间
        Dim nowTime As DateTime
        nowTime = nowTime.Now
        Console.WriteLine("开始显示时间:" + nowTime.Hour.ToString() + _
                            ":" + nowTime.Minute.ToString() + ":" + _
                            nowTime.Second.ToString())
        '构造定时器,1秒钟后开始周期性地调用方法,周期为2秒
        Dim timerCallback1 As TimerCallback
        timerCallback1 = New TimerCallback(AddressOf classObject.TimerMethod)
        Dim timer1 As Timer
        timer1 = New Timer(timerCallback1, classObject, 1000, 2000)
        '主线程休眠10秒钟
        Thread.Sleep(10000)
        '销毁定时器
        timer1.Dispose()
        Console.WriteLine("定时器运行结束且已被销毁,按回车键退出程序!")
        Console.ReadLine()
    End Sub
    Public Class Class1
        Public Sub TimerMethod(ByVal Obj As Object)
            '输出当前时间
            Dim nowTime As DateTime
            nowTime = nowTime.Now
            Console.WriteLine(nowTime.Hour.ToString() + ":" + _
                            nowTime.Minute.ToString() + ":" + _
                            nowTime.Second.ToString())
        End Sub
    End Class
End Module

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?