Locking Functions due to Multi-Threading in Visual Basic 6 Possible?

Posted on 2003-03-05
Medium Priority
Last Modified: 2013-11-25
Is theere a way to lock a function (or rather, a selection of code) from being executed by more than 1 thread.  I noticed there was a SyncLock command in VB .NET but this does not exist for VB6.  Can someone give me the deatails so i can implement the same kind of feature.
If it is of importance i am calling a function that writes out to a file.  The problem is that there are many threads running that could call this same function to write out to the file (append to it).  Since i could actually be writing to the file before i am interrupted by another thread which causes it to begin writing you can see it can end up being a mess without the lock feature

Question by:aniston
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 48

Accepted Solution

Mikal613 earned 80 total points
ID: 8071745
how about CreateMutex or Semaphore you can create a "Lock" on a thread untill the process ended.

Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long

Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Form_Load()
    Dim hMutex As Long
    'Try to create a new Mutex
    hMutex = CreateMutex(ByVal 0&, 1, App.Title)
    'Did the mutex already exist?
    If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then

        'Then Dont write to the file.-----

        ReleaseMutex hMutex
        CloseHandle hMutex
          exit sub
        Write to the File
    End If
End Sub

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question