VB6.0中使用Timer控件模拟多线程,如何对调用的通用函数加锁?

VB里我用Timer模拟多线程,线程内要调用一个函数,需要对这个函数进行锁定,直到该线程释放再被其他线程调用,如何对这个函数加锁?关键字是什么?有没有实例啥的。

弄一个全局整形变量gLock,0表示空闲,1表示假线程1在使用,2表示假线程2在使用,……
假线程n在调用不能由多个假线程调用的函数前,添加
Dim after As Double
Dim Integer gLock
gLock=0
‘……
after = Now + 10# / 24# / 3600#
Do
DoEvents
if gLock=0 then
gLock=n
if gLock=n then
call yourfunc()
gLock=0
endif
else
If Now > after Then
exit do
endif
endif
Loop

简单粗暴的做法是函数外设置个状态值。

Dim stat As Boolean

Private Sub Command1_Click()
    stat = Not stat
    Timer1.Interval = 500
    Timer1.Enabled = stat
    Timer2.Interval = 200
    Timer2.Enabled = stat
    
    
End Sub


Sub doit(i)
    stat = False
    s = DateTime.Timer
    Debug.Print i & "  " & s
    Do
        t = DateTime.Timer
        DoEvents
    Loop Until (t - s > 2)
    stat = True
End Sub

Private Sub Form_Load()
    stat = False
End Sub

Private Sub Timer1_Timer()
    If stat = True Then doit 1
End Sub

Private Sub Timer2_Timer()
    If stat = True Then doit 2
End Sub