Results 1 to 40 of 104

Thread: [VB6] - Module for working with multithreading.

Hybrid View

  1. #1
    Member
    Join Date
    Oct 2018
    Posts
    63

    Re: [VB6] - Module for working with multithreading.

    Hello The trick,

    Thank you very much for your project. It's helped a lot here

    Now I need an anointing to stop the threads immediately without waiting for them to finish.

    I tried the code below and things seem to be working fine. Please, do you have any other suggestions?

    Code:
    Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
    
    Public Function StopThreadImmediately(ByVal lAsynchId As Long) As Long
        Dim hThread As Long
        Const THREAD_TERMINATE As Long = 1
        ' // Unsupported in IDE
        If bIsInIDE Then Exit Function
            If lAsynchId > 0 Then
                hThread = OpenThread(THREAD_TERMINATE, 0, lAsynchId)
                If Not (hThread = 0) Then
                    StopThreadImmediately = TerminateThread(hThread, 0)
                    CloseHandle hThread
                End If
            End If
    End Function

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,797

    Re: [VB6] - Module for working with multithreading.

    Quote Originally Posted by hennyere View Post
    Hello The trick,

    Thank you very much for your project. It's helped a lot here

    Now I need an anointing to stop the threads immediately without waiting for them to finish.

    I tried the code below and things seem to be working fine. Please, do you have any other suggestions?

    Code:
    Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
    
    Public Function StopThreadImmediately(ByVal lAsynchId As Long) As Long
        Dim hThread As Long
        Const THREAD_TERMINATE As Long = 1
        ' // Unsupported in IDE
        If bIsInIDE Then Exit Function
            If lAsynchId > 0 Then
                hThread = OpenThread(THREAD_TERMINATE, 0, lAsynchId)
                If Not (hThread = 0) Then
                    StopThreadImmediately = TerminateThread(hThread, 0)
                    CloseHandle hThread
                End If
            End If
    End Function
    It's the bad practice to use TerminateThread because there is no resource releasing in this case. It's better to notify a thread (for example PostMessage/PostThreadMessage in certain cases) you want to finish it. If your thread is in waiting state you can use alertable state and notify by posting an APC request.

  3. #3
    Member
    Join Date
    Oct 2018
    Posts
    63

    Re: [VB6] - Module for working with multithreading.

    Quote Originally Posted by The trick View Post
    It's the bad practice to use TerminateThread because there is no resource releasing in this case. It's better to notify a thread (for example PostMessage/PostThreadMessage in certain cases) you want to finish it. If your thread is in waiting state you can use alertable state and notify by posting an APC request.
    Hi The Trick,

    I'm glad to see you here while I'm having a coffee.

    Thanks for letting me know about using TerminateThread.

    I'm trying to adapt your module into a project, and I would like to ask a question:

    In a very simple approach, we have some sql statements that need to be executed asynchronously. Please keep in mind that this is a summary code, therefore the RunSqlAssync function is called in several places in the project.

    Code:
    Form1.frm
    
    Private Sub Form_Load()
        modMultiThreading.Initialize
        modMultiThreading.EnablePrivateMarshaling True
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        modMultiThreading.Uninitialize
    End Sub
    
    Private Sub Command1_Click()
        RunSqlAssync "UPDATE mytable SET myfield1=100 WHERE myfield1=1000;"
        RunSqlAssync "UPDATE mytable SET myfield1=200 WHERE myfield1=2000;"
    End Sub
    Code:
    Module1.bas
    
    Public Sub RunSqlAssync(ByVal xSql As String) 
        Dim zTObj As Object, zTAsynchID As Long
        Set zTObj = modMultiThreading.CreatePrivateObjectByNameInNewThread("VBThreadClass", , zTAsynchID)
        modMultiThreading.AsynchDispMethodCall zTAsynchID, "RunSqlEx", VbMethod, Nothing, "", xSql
    End Function
    Code:
    VBThreadClass.cls
    
    Public Sub RunSqlEx(ByVal xSql As String)
        Dim xCmd As Object
        Set xCmd = CreateObject("ADODB.Command")
        xCmd.ActiveConnection = Db
        xCmd.CommandType = adCmdText
        xCmd.CommandText = xSql
        xCmd.Execute , , adExecuteNoRecords
        Set xCmd = Nothing
    End Sub

    As you noticed, every time I call RunSqlAssync, this Sub asynchronously executes RunSqlEx. This seems to work fine, but I have 2 questions:

    1. What happens to the Thread when RunSqlEx reaches the end? Is it simply finished?
    2. How many Threads can be executed in parallel?

    I've been working with C# for years and don't have much experience with Vb6.
    Last edited by hennyere; Apr 24th, 2024 at 07:12 AM.

  4. #4

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width