VB.Net创建一个ImageGenerator进程

时间:2015-08-16 21:06:52

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

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

我实际上是这样做的,就像这样:

        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

但是这会让我的程序崩溃并使用大量内存。

在此过程中我无法显示任何内容,例如:经过时间。计时器永远不会启动。

抱歉英文不好。

1 个答案:

答案 0 :(得分:0)

我不清楚您如何设置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()