Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:22:54+00:00 2026-05-27T15:22:54+00:00

I have a vb6 multithreaded app working and i would like to use mutexes

  • 0

I have a vb6 multithreaded app working and i would like to use mutexes to protect data. The expected behavior is that when a thread tried to obtain a lock on an existing mutex, when the “WaitForSingleObject” function is called, that thread blocks until the mutex is signaled. What I am experiencing is the entire app freezes.

To duplicate my project, open VB6 and create a new Active X EXE. Create a module with default name. Place this code in it:

Option Explicit

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub Main()
    ' this hack is necessary to ensure that we only 'create' the application window once..
    Dim hwnd As Long
    hwnd = FindWindow(vbNullString, "Form1")
    If hwnd = 0 Then
        Dim f As Form1
        Set f = New Form1
        f.Show
        Set f = Nothing
    End If
End Sub

Next create a class, with default name and add this code to it:

Option Explicit

Private Const INFINITE = -1&
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const SYNCHRONIZE As Long = &H100000
Private Const MUTANT_QUERY_STATE As Long = &H1
Private Const MUTANT_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or MUTANT_QUERY_STATE)

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As Any, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function OpenMutex Lib "kernel32" Alias "OpenMutexA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long

Private Const MUTEX_NAME As String = "mymutex"
Private m_hCurrentMutex As Long

Public Sub Class_Terminate()
    Call ReleaseIt
End Sub

Public Sub LockIt(success As String)
    Dim hMutex          As Long

    MsgBox "Lockit t:" & App.ThreadID
    hMutex = OpenMutex(STANDARD_RIGHTS_REQUIRED, 0, MUTEX_NAME)
    If hMutex <> 0 Then
        Form1.Caption = "waiting on mutex"
        MsgBox "waiting t:" & App.ThreadID
        Dim res As Long
        Do
            'MsgWaitForMultipleObjects
            res = WaitForSingleObject(hMutex, INFINITE)
            DoEvents
        Loop While res = -1
        m_hCurrentMutex = hMutex
    Else
        Form1.Caption = "creating mutex"
        m_hCurrentMutex = CreateMutex(ByVal 0&, 1, MUTEX_NAME)
    End If
    Form1.Caption = success
    MsgBox success
End Sub

Public Sub ReleaseIt()
    If m_hCurrentMutex <> 0 Then
        Call ReleaseMutex(m_hCurrentMutex)
        Call CloseHandle(m_hCurrentMutex)
        m_hCurrentMutex = 0
    End If
End Sub

Finally, in the main form, add 4 command buttons and this code:

Option Explicit
Dim c(1) As Class1

'Lock
Private Sub Command1_Click()
    If c(0) Is Nothing Then Set c(0) = CreateObject("Project1.Class1")
    Call c(0).LockIt("Object0")
End Sub
Private Sub Command2_Click()
    If c(1) Is Nothing Then Set c(1) = CreateObject("Project1.Class1")
    Call c(1).LockIt("Object1")
End Sub


'Free
Private Sub Command3_Click()
    If c(0) Is Nothing Then Set c(0) = CreateObject("Project1.Class1")
    Call c(0).ReleaseIt
End Sub
Private Sub Command4_Click()
    If c(1) Is Nothing Then Set c(1) = CreateObject("Project1.Class1")
    Call c(1).ReleaseIt
End Sub


Private Sub Form_Unload(Cancel As Integer)
    Set c(0) = Nothing
    Set c(1) = Nothing
    End
End Sub

The first two command buttons lock their respective mutexes. The 2nd two free it. Notice how before the mutex is locked, a unique thread id is displayed. This made me believe only that thread should block, and not freeze the entire application.

Any assistance would be greatly appreciated. Thank you.

EDIT: I forgot to mention a very important part: In the project properties section, I have it set to create ‘thread per object’ and this is verified with results of the msghox App.ThreadID calls.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:22:55+00:00Added an answer on May 27, 2026 at 3:22 pm

    While you can make a class create another thread (Using the ActiveX EXE hack) that you still have a single execution thread, i.e. all calls are serialised.

    If you want an asynchronous call cross thread, you need to set a timer (SetTimer() API) in that function and wait for the callback before doing the long running code. Also note that while that thread is locked you can not make ANY calls into it unless they can break and call DoEvents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VB6 App that uses a .Net component (via a .tlb reference
I have a VB6 app that I've been selling for over 12 years. Sometimes
I have a VB6 app that formerly worked perfectly on a Vista machine as
I have a project with some C# dlls that use other vb6 dlls. I
I have a VB6 app that loads initially (for a small prompt to enter
We have a VB6 app that launches our .NET code upon startup. Cold start
I have a VB6 program that someone recently helped me convert to VB.NET In
I have a VB6 dll that is trying to create a COM object using
I have a VB6 application that needs to update it's self. For this purpose,
I have a VB6 app. There is a main form. It houses a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.