投稿時間:2003/06/06(Fri) 19:54 投稿者名:Lantern
Eメール:
URL :
タイトル:VB6でのスレッド作成について
いつも参考にさせていただいています。
VB6でスレッドの作成ができるのか試しています。 デバック環境では一応の動作をするのですが、 コンパイル(P-Code,ネイティブコード共)すると メモリアクセスのエラーになります。
VBでのスレッド作成は不可能なのでしょうか? 環境:Win2000SP2 VB6.0SP5
試しているソースです。
'(Form1) Option Explicit
Private Sub Command1_Click()
Call Module1.CreateProcThread
End Sub
Private Sub Command2_Click() Call Module1.DeleteProcThread End Sub
'(Module1) Option Explicit '================================================= '= WinAPI定義 '================================================= Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type '構造体にNULLを遅れないのでLong型の0を送る 'Private Declare Function CreateThread Lib "kernel32" _ (lpThreadAttributes As SECURITY_ATTRIBUTES, _ ByVal dwStackSize As Long, _ lpStartAddress As Long, _ lpParameter As Any, _ ByVal dwCreationFlags As Long, _ lpThreadId As Long) As Long Private Declare Function CreateThread Lib "kernel32.dll" _ (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 Private Declare Function CloseHandle Lib "kernel32.dll" _ (ByVal hObject As Long) As Long Public Declare Function WaitForSingleObject Lib "kernel32.dll" _ (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Const WAIT_OBJECT_0 As Long = 0 Private hThread1 As Long 'スレッド1ハンドル Private hThread2 As Long 'スレッド2ハンドル Private dwThreadID1 As Long ' Private dwThreadID2 As Long ' Private bFlag As Boolean 'スレッド起動中のフラグ '================================================= '= '= 関数 :CreateProcThread '= 機能 :スレッドの作成 '= '================================================= Public Sub CreateProcThread()
bFlag = True hThread1& = CreateThread(0&, 0&, AddressOf ProcThread1, 0&, 0&, dwT hreadID1&) hThread2& = CreateThread(0&, 0&, AddressOf ProcThread2, 0&, 0&, dwT hreadID2&) End Sub '================================================= '= '= 関数 :DeleteProcThread '= 機能 :スレッドの破棄 '= '================================================= Public Sub DeleteProcThread()
bFlag = False Call CloseHandle(hThread1&) Call CloseHandle(hThread2&) End Sub '================================================= '= '= 関数 :ProcThread1 '= 機能 :スレッド1 '= '================================================= Public Sub ProcThread1()
Dim sx As Single, sy As Single Dim px As Single, py As Single Dim pr As Long, pg As Long, pb As Long sx! = Form1.Picture1.ScaleWidth sy! = Form1.Picture1.ScaleHeight Do px! = Rnd() * sx! py! = Rnd() * sy! pr& = Rnd() * 256 pg& = Rnd() * 256 pb& = Rnd() * 256 Form1.Picture1.PSet (px!, py!), RGB(pr&, pg&, pb&) Loop While bFlag End Sub '================================================= '= '= 関数 :ProcThread2 '= 機能 :スレッド2 '= '================================================= Public Sub ProcThread2()
Dim sx As Single, sy As Single Dim px1 As Single, py1 As Single Dim px2 As Single, py2 As Single Dim pr As Long, pg As Long, pb As Long
sx! = Form1.Picture2.ScaleWidth sy! = Form1.Picture2.ScaleHeight Do px1! = Rnd() * sx! py1! = Rnd() * sy! px2! = Rnd() * sx! py2! = Rnd() * sy! pr& = Rnd() * 256 pg& = Rnd() * 256 pb& = Rnd() * 256 Form1.Picture2.Line (px1!, py1!)-(px2!, py2!), RGB(pr&, pg&, pb&), B Loop While bFlag End Sub
|