MUTEX-请帮助 [英] MUTEX - Help please

查看:68
本文介绍了MUTEX-请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好全部,

又是我.

我需要利用此功能的MUTEX类的作用是在更新数据库时隔离线程,以便其他线程在运行 线程不会干扰并等待它们轮流更新相同的Db和表.
我一直在使用Google搜索,

MUTEX是否替换了我现有的BGW或它是在我的BGW内部实现的吗?我一直在尝试从中复制一些示例
这是我的问题.

读取多个磁盘(每个线程一个)并存储数据.
当线程读取数据时,必须更新数据库,然后读取更多数据.这一切都相对较快,直到我添加了 插入记录"代码.每个线程在读取数据方面都做得很好.
我为所选的每个磁盘创建了一个BGW在我的DGV中.我如何在DoWork中利用MUTEX?
我知道这可能是不好的礼节,但是如果可以的话,您能帮我提供MUTEX的代码示例吗?

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

        Try

            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
                Workers(NumWorkers).RunWorkerAsync()

            Next

        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

推荐答案

您是否查看了

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.


这篇关于MUTEX-请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆