Hab mir das Ganze mal angeschaut und habe eine Lösung. Um den Thread zu beenden und gleichzeitig auch das Programm brauchst Du einen Timer auf der Form.
Hier mal der modifizierte Code:
Modul:
Option Explicit
Public Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, _
ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, _
ByVal dwCreationFlag As Long, lpThreadId As Long) As Long
Public Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As _
Long
Public Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As _
Long
Public Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As _
Long, ByVal nPriority As Long) As Long
Declare Sub ExitThread Lib "kernel32.dll" (ByVal dwExitCode As Long)
Public Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, _
ByVal dwExitCode As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliSeconds As Long)
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As _
Long
Public hWatchTh&, WatchThId&
Public Threadbeenden As Boolean
Public Const CREATE_SUSPENDED = &H4
Public Const THREAD_PRIORITY_IDLE = -15
Public Const THREAD_PRIORITY_TIME_CRITICAL = 15
Public Const THREAD_PRIORITY_NORMAL = 0
Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) _
As Long
Public Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As _
Long, lpExitCode As Long) As Long
Public Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal x1 As _
Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long) As Long
Public lpExCode As Long
Public bThreadRunning As Boolean
Public Function test_tr(p As Long) As Long
While Not Threadbeenden
Form1.Picture1.ForeColor = QBColor(Int(14 * Rnd))
Ellipse Form1.Picture1.hdc, Rnd * 150, Rnd * 150, Rnd * 150, Rnd * 150
Wend
test_tr = 1
bThreadRunning = False
End Function Form:
Option Explicit
Private Sub Command1_Click()
hWatchTh = CreateThread(ByVal 0&, 0, AddressOf test_tr, ByVal 0&, _
CREATE_SUSPENDED, WatchThId)
SetThreadPriority hWatchTh, THREAD_PRIORITY_NORMAL
Threadbeenden = False
bThreadRunning = True
ResumeThread hWatchTh
End Sub
Private Sub Command2_Click()
Threadbeenden = True
End Sub
Private Sub Command3_Click()
Threadbeenden = True
Timer1.Enabled = True
End Sub
Private Sub Command4_Click()
Unload Me
End
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 50
End Sub
Private Sub Timer1_Timer()
If Not bThreadRunning Then
Timer1.Enabled = False
Unload Me
End
End If
End Sub _________________________
Professionelle Entwicklerkomponenten
www.tools4vb.de |