背景工人或锁定线程的最佳方法

时间:2016-02-11 23:48:31

标签: backgroundworker

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

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秒钟,我想杀死该线程,然后删除当前模型副本“通用”副本并让程序继续。

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

我确信我并没有把这一切描述得很好,但感谢任何支持批评或我可能收到的任何批评。

0 个答案:

没有答案