【问题标题】:VB.Net Create a ImageGenerator processVB.Net 创建 ImageGenerator 进程
【发布时间】:2023-03-24 15:18:01
【问题描述】:

我想知道如何制作流程..

我实际上是用这样的 For 来做这个的:

        timeCount = 0
        Timer.Start()
        Dim Count As Integer = 1, next As Boolean
        For Each dataRow As DataRow In DataBase.Rows
            nextImage = False
            Do While nextImage = False
                Try
                    Dim Ext As String = ".png"
                    If rbJPG.Checked = True Then Ext = ".jpg"
                    GenerateImage(dataRow, ImageFrom, FBD.SelectedPath, Ext, Count)
                    nextImage = True
                Catch ex As Exception
                    Timer.Stop()
                    Dim Result As DialogResult = MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)
                    If Result = Windows.Forms.DialogResult.Ignore Then
                        Timer.Start()
                        nextImage = True
                    ElseIf Result = Windows.Forms.DialogResult.Retry Then
                        Timer.Start()
                        nextImage = False
                    ElseIf Result = Windows.Forms.DialogResult.Abort Then
                        prgGenerate.Value = 0
                        Exit For
                    End If
                End Try
            Loop
            Count += 1
            prgGenerate.Increment(1)
        Next

但这会使我的程序崩溃并使用大量 RAM。

在这个过程中我什么都不能显示,例如:Elapsed Time。而且计时器永远不会启动。

抱歉英语不好。

【问题讨论】:

  • 你需要把那个“工作”放到不同的线程中。看看使用 BackgroundWorker() 控件。另外,Timer 实际上在做什么?...
  • 计算秒数..我用它来获取经过的时间。

标签: vb.net image for-loop process generator


【解决方案1】:

我不清楚您是如何设置 ImageFrom 变量的,但这里有一个使用 BackgroundWorker() 的简单示例:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    timeCount = 0
    Me.Invoke(Sub()
                  Timer.Start()
              End Sub)
    Dim Count As Integer = 1, nextImage As Boolean
    For Each dataRow As DataRow In Database.Rows
        nextImage = False
        Do While nextImage = False
            Try
                Dim Ext As String = ".png"
                If rbJPG.Checked = True Then Ext = ".jpg"
                GenerateImage(dataRow, ImageFrom, FBD.SelectedPath, Ext, Count)
                nextImage = True
            Catch ex As Exception
                Me.Invoke(Sub()
                              Timer.Stop()
                              Dim Result As DialogResult = MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)
                              If Result = Windows.Forms.DialogResult.Ignore Then
                                  Timer.Start()
                                  nextImage = True
                              ElseIf Result = Windows.Forms.DialogResult.Retry Then
                                  Timer.Start()
                                  nextImage = False
                              ElseIf Result = Windows.Forms.DialogResult.Abort Then
                                  prgGenerate.Value = 0
                                  Exit For
                              End If
                          End Sub)
            End Try
        Loop
        Count += 1
        Me.Invoke(Sub()
                      prgGenerate.Increment(1)
                  End Sub)
    Next
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MessageBox.Show("Done!")
End Sub

您可以使用以下方式启动后台线程:

BackgroundWorker1.RunWorkerAsync()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多