MUTEX - Help please RRS feed

  • Question

  • Hello All,

    It's me again.

    I have a need to leverage the ability of the MUTEX Class to sequester a thread while it's updating a DB, so that the other running threads will not interfere and wait their turn to update the same Db and Table.

    I have been googling and I'm just not getting it.

    Does the MUTEX replace my existing BGW's or is it implemented from within my BGW? I have been trying to copy some examples from the net and get them to run, but I'm having absolutely no luck what so ever.

    Here is my issue.

    Read multiple disks (one per thread) and store data. This can be anywhere from 1 thread to 64+ depending on the CPU's.

    As a thread reads the data, it must update a database then read some more data. This all happens relatively fast until I add the "Insert Record" Code. Each thread does a great job of reading data. They just do not play nice when it comes to doing anything meaningful with it.

    I create one BGW for every Disk selected in my DGV. How do I leverage MUTEX within my DoWork?

    I know it may be poor etiquette , but if some one could, would you help me out with a code sample of MUTEX?

      Dim Rowcount As Int16 = QueueDGV.Rows.Count
                For x = 1 To (Rowcount - 1)
                    'Capture current row for DoWork
                    RowtoProcess = x - 1
                    NumWorkers = NumWorkers + 1
                    ReDim Workers(NumWorkers)
                    Workers(NumWorkers) = New BackgroundWorker
                    Workers(NumWorkers).WorkerReportsProgress = True
                    Workers(NumWorkers).WorkerSupportsCancellation = True
                    AddHandler Workers(NumWorkers).DoWork, AddressOf WorkerDoWork
                    AddHandler Workers(NumWorkers).ProgressChanged, AddressOf WorkerProgressChanged
                    AddHandler Workers(NumWorkers).RunWorkerCompleted, AddressOf WorkerCompleted
            Catch ex As Exception
                MessageBox.Show("Threading Erorr : " + vbCrLf + ex.Message)
            End Try
        Private Sub WorkerDoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
        End Sub
        Private Sub WorkerProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs)
        End Sub
        Private Sub WorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs)
        End Sub

    Thursday, March 23, 2017 2:55 PM

All replies

  • Did you look at the doc for the Mutex Class. The example looks rather straightforward. Basically you use it to block access to the non re-entrant code, which would be your data access code. I'm not sure if I fully understand everything your BackgroundWorker threads are doing but ultimately you are single threading the database operation with the Mutex thread.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Frank L. Smith Thursday, March 23, 2017 5:08 PM
    Thursday, March 23, 2017 3:59 PM