home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD170663142001.psc / clsThreading.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-03-15  |  6.9 KB  |  188 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsThreading"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. '----------------------------------------------------------------------------------------'
  15. '
  16. ' Multi Downloader using multithreadings
  17. ' Created by Suk Yong Kim, 03/14/2001
  18. '
  19. ' This project is my first project to upload to the PSC.
  20. ' Many persons contribute to create this project
  21. ' I really appreicate their efforts and codes and the great server PSC.
  22. '
  23. ' if any question, mail to : techtrans@dreamwiz.com
  24. ' This class moduesis contributed by Philipp Weidmann
  25. ' I really thank him for his codes.
  26. '----------------------------------------------------------------------------------------'
  27. 'clsThreading:
  28. 'Simple class that allows you to implement multithreading in your app
  29. '
  30. '(C) 2001 by Philipp Weidmann
  31.  
  32. 'API Declarations
  33. 'Creates a new thread
  34. Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
  35. 'Terminates a thread
  36. Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
  37. 'Sets the priority of a thread
  38. Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
  39. 'Returns the proirity of a thread
  40. Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long
  41. 'Enables a disabled Thread
  42. Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
  43. 'Disables a thread
  44. Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long
  45. 'Returns the handle of the current thread
  46. Private Declare Function GetCurrentThread Lib "kernel32" () As Long
  47. 'Returns the ID of the current thread
  48. Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
  49.  
  50. 'Consts
  51. Private Const MAXLONG = &H7FFFFFFF
  52.  
  53. 'Thread priority consts
  54. Private Const THREAD_BASE_PRIORITY_IDLE = -15
  55. Private Const THREAD_BASE_PRIORITY_LOWRT = 15
  56. Private Const THREAD_BASE_PRIORITY_MAX = 2
  57. Private Const THREAD_BASE_PRIORITY_MIN = -2
  58. Private Const THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAX
  59. Private Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MIN
  60. Private Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)
  61. Private Const THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)
  62. Private Const THREAD_PRIORITY_ERROR_RETURN = (MAXLONG)
  63. Private Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLE
  64. Private Const THREAD_PRIORITY_NORMAL = 0
  65. Private Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRT
  66.  
  67. 'Thread creation flags
  68. Private Const CREATE_ALWAYS = 2
  69. Private Const CREATE_NEW = 1
  70. Private Const CREATE_NEW_CONSOLE = &H10
  71. Private Const CREATE_NEW_PROCESS_GROUP = &H200
  72. Private Const CREATE_NO_WINDOW = &H8000000
  73. Private Const CREATE_PROCESS_DEBUG_EVENT = 3
  74. Private Const CREATE_SUSPENDED = &H4
  75. Private Const CREATE_THREAD_DEBUG_EVENT = 2
  76.  
  77. 'Types and Enums
  78. Public Enum ThreadPriority
  79.     tpLowest = THREAD_PRIORITY_LOWEST
  80.     tpBelowNormal = THREAD_PRIORITY_BELOW_NORMAL
  81.     tpNormal = THREAD_PRIORITY_NORMAL
  82.     tpAboveNormal = THREAD_PRIORITY_ABOVE_NORMAL
  83.     tpHighest = THREAD_PRIORITY_HIGHEST
  84. End Enum
  85.  
  86. 'Vars
  87. Private mThreadHandle As Long
  88. Private mThreadID As Long
  89. Private mPriority As Long
  90. Private mEnabled As Boolean
  91. Private mCreated As Boolean
  92. Private mID As Long
  93. Public Function CreateNewThread(ByVal iDownloader As Long, ByVal cFunction As Long, Optional ByVal cPriority As Long = tpNormal, Optional ByVal cEnabled As Boolean = True)
  94.     'Creates a new Thread
  95.     Dim mHandle As Long
  96.     Dim CreationFlags As Long
  97.     Dim lpThreadID As Long
  98.     
  99.     'Look if the thread has already been created
  100.     If mCreated = True Then Exit Function
  101.     
  102.     'Look if the thread should be enabled
  103.     If cEnabled = True Then
  104.         CreationFlags = 0
  105.     Else
  106.         'Create a disabled thread, can be enabled later with the
  107.         ''Enabled' property
  108.         CreationFlags = CREATE_SUSPENDED
  109.     End If
  110.     
  111.     'The CreateThread Function returns the handle of the created thread;
  112.     'if the handle is 0, it failed creating the thread
  113.     mHandle = CreateThread(ByVal 0&, ByVal 0&, cFunction, ByVal 0&, CreationFlags, lpThreadID)
  114.     
  115.     If mHandle = 0 Then 'Failed creating the thread
  116.         'Insert your own error handling
  117.         'Debug.Print "InitializeThread Function in clsThreading failed creating a new thread"
  118.     Else
  119.         mThreadHandle = mHandle
  120.         mThreadID = lpThreadID
  121.         mCreated = True
  122.         mID = iDownloader
  123.     End If
  124. End Function
  125. Public Property Get iDownloader() As Long
  126.     iDownloader = mID
  127. End Property
  128. Public Function TerminateCurrentThread()
  129.     'Terminates the current thread
  130.     
  131.     'Ignore errors to prevent crashing if no thread has been created
  132.     On Error Resume Next
  133.     'Terminate the thread to prevent crashing if the app is closed
  134.     'and the thread is still running (dangerous!)
  135.     Call TerminateThread(mThreadHandle, ByVal 0&)
  136.     mCreated = False
  137. End Function
  138.  
  139. Public Property Get ThreadHandle() As Long
  140.     'Returns the Handle of the current Thread
  141.     ThreadHandle = mThreadHandle
  142. End Property
  143.  
  144. Public Property Get ThreadID() As Long
  145.     'Returns the ID of the current thread
  146.     ThreadID = mThreadID
  147. End Property
  148.  
  149. Public Property Get Priority() As Long
  150.     'Returns a long value because the thread might have other priorities
  151.     'than our five in the enum
  152.     
  153.     'Ignore errors to prevent crashing if no thread has been created
  154.     On Error Resume Next
  155.     Priority = GetThreadPriority(mThreadHandle)
  156. End Property
  157.  
  158. Public Property Let Priority(ByVal tmpValue As Long)
  159.     'Sets the Thread Priority of the actual thread
  160.     mPriority = tmpValue
  161.     Call SetThreadPriority(mThreadHandle, tmpValue)
  162. End Property
  163.  
  164. Public Property Get Enabled() As Boolean
  165.     'Returns whether the Thread is enabled or not
  166.     Enabled = mEnabled
  167. End Property
  168.  
  169. Public Property Let Enabled(ByVal tmpValue As Boolean)
  170.     'Enables/Disables the Thread
  171.     
  172.     'Ignore errors to prevent crashing if no thread has been created
  173.     On Error Resume Next
  174.     If tmpValue = True Then
  175.         'Enable the thread
  176.         Call ResumeThread(mThreadHandle)
  177.     ElseIf tmpValue = False Then
  178.         'Disable the thread
  179.         Call SuspendThread(mThreadHandle)
  180.     End If
  181. End Property
  182.  
  183. Private Sub Class_Terminate()
  184.     'Terminate the thread to prevent crashing if the app is closed
  185.     'and the thread is still running (dangerous!)
  186.     Call TerminateCurrentThread
  187. End Sub
  188.