Results 1 to 40 of 104

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

Threaded View

  1. #31
    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.

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