Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backgroundworker 后台工作线程或锁定线程的最佳方法_Backgroundworker - Fatal编程技术网

Backgroundworker 后台工作线程或锁定线程的最佳方法

Backgroundworker 后台工作线程或锁定线程的最佳方法,backgroundworker,Backgroundworker,我们正在使用VB.net 2015。当应用程序加载时,它将检查模型文件是否存在,然后加载该文件进行处理。即 If frmMain.chkTMQEnableHortizontalScaling.Checked Then ShapeModel = TMTrainImage.CreateAnisoShapeModel(4, -10 / Rad, 20 / Rad, "auto", RScaleMin, RScaleMax, "auto", ScaleMin,CScaleMax, "auto", "n

我们正在使用VB.net 2015。当应用程序加载时,它将检查模型文件是否存在,然后加载该文件进行处理。即

If frmMain.chkTMQEnableHortizontalScaling.Checked Then

ShapeModel = TMTrainImage.CreateAnisoShapeModel(4, -10 / Rad, 20 / Rad, "auto", RScaleMin, RScaleMax, "auto", ScaleMin,CScaleMax, "auto", "none", "use_polarity", "auto", "auto")

Else

ShapeModel = TMTrainImage.CreateScaledShapeModel(4, -10 / Rad, 20 / ad, "auto", 1, 1, "auto", "none", "use_polarity", "auto", "auto")

End If
问题是,有时候模型会被破坏,它会锁定gui并不确定地停留在那里。因此,我决定对主要形式做一个背景介绍,并将其作为一个主题加以阐述

Private Sub BackgroundWorker_DoWork(sender As Object, err As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork

    BackgroundWorker.WorkerSupportsCancellation = True

    If BackgroundWorker.CancellationPending Then
        err.Cancel = True
    End If

    If chkTMQEnableHortizontalScaling.Checked Then
        ShapeModel = TMTrainImage.CreateAnisoShapeModel(4, -10 / Rad, 20 / Rad, "auto", RScaleMin, RScaleMax, "auto", CScaleMin, CScaleMax, "auto", "none", "use_polarity", "auto", "auto")
    Else
        ShapeModel = TMTrainImage.CreateScaledShapeModel(4, -10 / Rad, 20 / Rad, "auto", 1, 1, "auto", "none", "use_polarity", "auto", "auto")
    End If
    PartLoadSuccess = True
它在主循环中被调用

Dim tmPath As String
            Dim tmString
            Dim timeoutCount As Integer
            Dim timeoutValue As Integer = 5
            tmPath = CurrentFilePath & "Image1.Tiff"
            frmMain.BackgroundWorker.RunWorkerAsync()   'start worker frmmain.backgroundworker.dowork
            Do While frmMain.BackgroundWorker.IsBusy And Not timeout
                Thread.Sleep(1000)
                timeoutCount += 1   ' increment timeout value
                If PartLoadSuccess = True Then Exit Do
                If timeoutCount = timeoutValue Then
                    timeout = True ' connection timed out
                    frmMain.BackgroundWorker.CancelAsync()
                    frmMain.BackgroundWorker.Dispose()
                    frmMain.lblAlarmMessage.BackColor = System.Drawing.Color.Red
                    frmMain.lblAlarmMessage.Text = "()" & vbCr & "Timeout Loading  Image" & vbCr & Err.Description
                    If File.Exists(tmPath) Then
                        tmString = "Image " & Date.Now & ".tiff"
                        tmString = Replace(tmString, "/", "-")
                        tmString = Replace(tmString, ":", "-")
                        File.Move(tmPath, CurrentFilePath & tmString)
                        File.Copy(ConfigPath & "Default.tiff", CurrentFilePath & "Image.tiff")
                    End If
                    'timeout = False    ' connection timed out
                    PartLoad = False
                    LoadImage()
                    'Exit Sub
                End If
            Loop
            timeout = False

            Do While frmMain.BackgroundWorker.IsBusy And Not timeout
                Thread.Sleep(1000)
                timeoutCount += 1   ' increment timeout value
                If PartLoadSuccess = True Then Exit Do
                If timeoutCount = timeoutValue Then
                    frmMain.BackgroundWorker.CancelAsync()
                    frmMain.BackgroundWorker.Dispose()
                End If
            Loop
如果工作线程占用的时间超过5秒,我想终止该线程,然后删除当前模型,并在适当的位置复制一个“通用”线程,然后让程序继续

我能够顺利完成第一次迭代,但工作人员没有停止,我无法再次运行它


我确信我没有很好地描述这一切,但我非常感谢任何支持批评或我可能收到的任何东西。

您似乎正在启动BackgroundWorker,但却阻塞了UI线程,等待BackgroundWorker完成。如果没有有效的模型,程序将无法继续。我假设我正在启动BackgroundWorker。主线程将循环5秒,如果backgroundworker没有partloadsuccess,那么我会知道它已被锁定,这样我可以删除该模型文件,插入泛型并重新运行worker。最好禁用UI
frmMain.Enabled=false
,同时确保您有一个有效的模型,然后,一旦模型可用,将Enabled设置为true。这将允许UI线程继续泵送消息队列。您似乎正在启动BackgroundWorker,但却阻止了UI线程,等待BackgroundWorker完成。如果没有有效的模型,程序将无法继续。我假设我正在启动BackgroundWorker。主线程将循环5秒,如果backgroundworker没有partloadsuccess,那么我会知道它已被锁定,这样我可以删除该模型文件,插入泛型并重新运行worker。最好禁用UI
frmMain.Enabled=false
,同时确保您有一个有效的模型,然后,一旦模型可用,将Enabled设置为true。这将允许UI线程继续泵送消息队列。