博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VB6多线程,关键段操作
阅读量:5140 次
发布时间:2019-06-13

本文共 3458 字,大约阅读时间需要 11 分钟。

Option Explicit

Declare Function GetLastError Lib "kernel32" () As Long
'Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'Declare Sub ExitThread Lib "kernel32" (Optional ByVal dwExitCode As Long = 0)
'Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Declare Function CreateThreadL Lib "kernel32" Alias "CreateThread" (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Public Const CREATE_SUSPENDED = &H4
Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Public 结束所有线程操作 As Boolean
'Public 线程属性 As SECURITY_ATTRIBUTES
Public ID As Long, 句柄1 As Long, 句柄2 As Long, 参数 As Long
Public 共享变量 As Long
Public 线程数量 As Long
Public Declare Sub InitializeCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
Public Declare Sub DeleteCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
Private Declare Sub EnterCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
Private Declare Sub LeaveCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
Public Type CRITICAL_SECTION
    DebugInfo As Long
    LockCount As Long
    RecursionCount As Long
    OwningThread As Long
    Reserved As Long
End Type
Public g_cs As CRITICAL_SECTION
Public Sub 创建线程()
    线程数量 = 0
    
    结束所有线程操作 = False
    '线程属性.nLength = Len(线程属性)
    句柄1 = CreateThreadL(0, 0, AddressOf 线程函数1, 0&, CREATE_SUSPENDED, ID)
    句柄2 = CreateThreadL(0, 0, AddressOf 线程函数2, 0&, CREATE_SUSPENDED, ID)
    If 句柄1 <> 0 And 句柄2 <> 0 Then
        主窗体.Caption = "成功!句柄1:" & 句柄1 & ";句柄2:" & 句柄2 & ";ID:" & ID ' & ";参数:" & 参数
    Else
        主窗体.Caption = "失败!错误码:" & GetLastError
    End If
End Sub
Public Sub 启动线程()
    If ResumeThread(句柄1) = -1 Then
        主窗体.Caption = "失败!错误码:" & GetLastError
    End If
    If ResumeThread(句柄2) = -1 Then
        主窗体.Caption = "失败!错误码:" & GetLastError
    End If
End Sub
Public Sub 结束线程()
    Dim EndThread As Boolean
    Call EnterCriticalSection(g_cs)
        结束所有线程操作 = True
    Call LeaveCriticalSection(g_cs)
    Do
        DoEvents '奇怪,不能不加。可能处理全局变量仍然需要主线程的参与吧。
        Call EnterCriticalSection(g_cs)
            EndThread = (线程数量 <= 0)
        Call LeaveCriticalSection(g_cs)
    Loop Until EndThread
End Sub
Public Function 线程函数1(ByVal 参数 As Long) As Long
    Call EnterCriticalSection(g_cs)
        线程数量 = 线程数量 + 1
    Call LeaveCriticalSection(g_cs)
    Dim i As Long
    For i = 0 To 100000
        Call EnterCriticalSection(g_cs)
            If 结束所有线程操作 Then
                Call LeaveCriticalSection(g_cs)
                Exit For
            End If
            主窗体.tr1.Caption = i
            共享变量 = 共享变量 + 1
            主窗体.tr.Caption = 共享变量
        Call LeaveCriticalSection(g_cs)
    Next
    Call EnterCriticalSection(g_cs)
        主窗体.显示结束标语
        线程数量 = 线程数量 - 1
    Call LeaveCriticalSection(g_cs)
    '函数结束的时候,线程自然就结束了,不需要调用下面注释中的 ExitThread 函数。
    'ExitThread
End Function
Public Function 线程函数2(ByVal 参数 As Long) As Long
    Call EnterCriticalSection(g_cs)
        线程数量 = 线程数量 + 1
    Call LeaveCriticalSection(g_cs)
    Dim i As Long
    For i = 0 To 100000
        Call EnterCriticalSection(g_cs)
            If 结束所有线程操作 Then
                Call LeaveCriticalSection(g_cs)
                Exit For
            End If
            主窗体.tr2.Caption = i
            共享变量 = 共享变量 + 1
            主窗体.tr.Caption = 共享变量
        Call LeaveCriticalSection(g_cs)
    Next
    Call EnterCriticalSection(g_cs)
        主窗体.显示结束标语
        线程数量 = 线程数量 - 1
    Call LeaveCriticalSection(g_cs)
    '函数结束的时候,线程自然就结束了,不需要调用下面注释中的 ExitThread 函数。
    'ExitThread
End Function

转载于:https://www.cnblogs.com/MMLoveMeMM/articles/3187246.html

你可能感兴趣的文章
自己写的仿爱奇艺综艺频道轮播图,没有淡入淡出效果
查看>>
提炼游戏引擎系列:第一次迭代
查看>>
Django 学习
查看>>
Android的事件处理机制详解(二)-----基于监听的事件处理机制
查看>>
s5-12 RIP
查看>>
Linux-以指定用户运行redis
查看>>
Linux-socket的close和shutdown区别及应用场景
查看>>
初探Oracle全栈虚拟机---GraalVM
查看>>
移动端的点击滚动逻辑实现。
查看>>
xpath
查看>>
parted分区
查看>>
抛出错误
查看>>
Can't play local SWF file in Media Player
查看>>
图片标签img
查看>>
JavaScript语言中文参考手册.chm
查看>>
表哥的Access入门++以Excel视角快速学习数据库知识pdf
查看>>
day29 jq
查看>>
TC 配置插件
查看>>
关于异步reset
查看>>
索引优先队列的工作原理与简易实现
查看>>