seabird125(海鸥) ( ) 信誉:60 Blog 2006-12-5 15:40:55 得分: 0
重要是这个等待。。。
//
要求不高的话,就用循环吧.....
Public Running As Boolean '函数是否在执行
Public Function TestFun(Byval S1 as long,Byval S2 as long)as boolean
TestFun=False
if Running=True then
exit function
end if
Running=True
[你的处理过程->Start]
.
.
.
.
[你的处理过程->End]
Running=false
TestFun=True
End function
调用时:
do
if testfun(s1,s2)=True then exit do
sleep 1
loop
'in a form module
Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (ByVal lpMutexAttributes As Long, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'利用互斥内核对象进行互斥,保证只能有一个线程访问
Private Function GetOneNumber() As Long
Static lResult As Long
WaitForSingleObject m_hMutex, INFINITE
lResult = lResult + 1
ReleaseMutex m_humtex
GetOneNumber = lResult
End Function
Private Sub Form_Load()
'建立内核对象
m_hMutex = CreateMutex(0&, 0&, "Base_Mutex")
'建立两个计时器同时访问GetOneNumer(),进行测试
Timer1.Interval = 100
Timer2.Interval = 30
End Sub
'如果建立了Mutex互斥,则在窗体销毁时关闭内核对象
Private Sub Form_Unload(Cancel As Integer)
If m_hMutex <> 0 Then CloseHandle m_hMutex
End Sub
'计时器事件
Private Sub Timer1_Timer()
Text1.Text = GetOneNumber() & vbCrLf & Text1.Text
End Sub
Private Sub Timer2_Timer()
Text2.Text = GetOneNumber() & vbCrLf & Text2.Text
End Sub