简体   繁体   English

将vb中的代码块转换为c#时,应该将其放在循环的哪一部分?

[英]What part from the block of code in vb when converting it to c# should i put in the loop?

This is the code in vb: 这是vb中的代码:

Loop until done 循环直到完成

Loop Until prcFFMPEG.HasExited And strFFOUT = Nothing Or strFFOUT = ""

    If ProgressBar1.Value <> ProgressBar1.Maximum Then
        ProgressBar1.Value = ProgressBar1.Maximum   'make the progressbar hit its maximum 
                        'on completion
    End If

    MsgBox("The Conversion Process has Completed.", _
        MsgBoxStyle.Information, "Finished.")   'display on completion

    Button5.Enabled = False     'Disable the cancel button
    Button3.Enabled = True      'Reenable the convert button
    ProgressBar1.Value = 0      'reset the progressbar value to 0
    Label11.Text = _ProgressBar1.Value & "%"     'get the progressbar value 
                        'and report it as percent for visual purposes

    Label12.Text = "Current Encoded Frame: "
    If Me.Height = 508 Then     'if the show detailed information button 
                    'was pressed reset the form height back to normal
        Me.Height = 441
        RichTextBox1.Visible = False    'Disable the richtextbox
    End If

So i converted the loop to c# like this: If the convertion to c# is right then what part of the code i should put in the loop ? 因此,我将循环转换为c#,如下所示:如果正确转换为c#,则应将代码的哪一部分放入循环中?

 while(prcFFMPEG.HasExited && strFFOUT == "")
                {

                }

Only the two lines( i will convert the lines later to c# ) 只有两行(我稍后会将这两行转换为c#)

 If ProgressBar1.Value <> ProgressBar1.Maximum Then
            ProgressBar1.Value = ProgressBar1.Maximum

Or all the code to put in the loop ? 还是将所有代码放入循环中?

This is the original method in vb where the loop is in: 这是vb中循环所在的原始方法:

Private Sub Convert()   ' function Convert() here is the actuall conversion process

        Control.CheckForIllegalCrossThreadCalls = False   'Check for illegal cross threads      
        ''''''''''''''''
        'set presets here 'Note Combobox indexes start at 0 not 1  
        'mobile Devices
        If ComboBox1.SelectedIndex = 3 Then ' 480x320 option

            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 480x320 -pix_fmt yuv420p -qp 23 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 4 Then ' 640x480 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 640x480 -pix_fmt yuv420p -qp 22 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 5 Then ' 800x600 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 800x600 -pix_fmt yuv420p -qp 21 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 6 Then ' 1024x768 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1024x768 -pix_fmt yuv420p -qp 20 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 7 Then ' 1280x720 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 19 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 8 Then ' 1920x1080 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1920x1080 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        ''''''''''''''
        If ComboBox1.SelectedIndex = 12 Then ' Ps3 720p option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 13 Then ' ps3 1080p option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1920x1080 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        ''''''''''''''
        If ComboBox1.SelectedIndex = 17 Then ' TV Standard Def 720x576p PAL AVI option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v mpeg4 -s 720x576 -pix_fmt yuv420p -b:v 1500k -r 30 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 18 Then ' TV Standard Def 720x576p PAL MP4 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 720x576 -pix_fmt yuv420p -qp 19 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        ''''''''''''''
        If ComboBox1.SelectedIndex = 19 Then ' TV High Def 1280x720p PAL AVI option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v mpeg4 -s 1280x720 -pix_fmt yuv420p -b:v 2500k -r 30 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 20 Then ' TV High Def 1280x720p PAL MP4 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        ''''''''''''''
        If ComboBox1.SelectedIndex = 21 Then ' TV High Def 1920x1080p PAL AVI option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v mpeg4 -s 1920x1080 -pix_fmt yuv420p -b:v 3000k -r 30 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 22 Then ' TV High Def 1920x1080p PAL MP4 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1920x1080 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If

        '''''''''''''''''''

        If ComboBox1.SelectedIndex = 26 Then ' MKV 1 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -b:v 3000k -r 30 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 27 Then ' MKV 2 option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1920x1080 -pix_fmt yuv420p -b:v 4000k -r 30 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 28 Then 'SD flv option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 480x320 -pix_fmt yuv420p -b:v 1000k -r 30 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If

        ''''''''''''''''
        If ComboBox1.SelectedIndex = 30 Then 'low qual flv option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 320x240 -pix_fmt yuv420p -b:v 800k -r 30 -profile high444 -c:a libvo_aacenc -b:a 96k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 31 Then 'medium qual flv option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 480x320 -pix_fmt yuv420p -b:v 1000k -r 30 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 32 Then 'high qual flv option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 720x576 -pix_fmt yuv420p -b:v 1500k -r 30 -profile high444 -c:a libvo_aacenc -b:a 192k -ar 44100 -ac 2 -y " & OutputFile
        End If

        ''''''''''''''''
        If ComboBox1.SelectedIndex = 34 Then    '320kbs mp3 only option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -vn -c:a libmp3lame -b:a 320k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 35 Then    '192kbs mp3 only option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -vn -c:a libmp3lame -b:a 192k -ar 44100 -ac 2 -y " & OutputFile
        End If
        If ComboBox1.SelectedIndex = 36 Then    '128kbs mp3 only option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -vn -c:a libmp3lame -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
        End If

        If ComboBox1.SelectedIndex = 38 Then   'Lossless Copy option
            strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -qp 0 -c:a libvo_aacenc -b:a 320k -ar 44100 -ac 2 -y " & OutputFile
        End If



        '''''''''''''''''
        'start Main Process
        psiProcInfo.FileName = Application.StartupPath + "\ffmpeg.exe"  'Location Of FFMPEG.EXE
        psiProcInfo.Arguments = strFFCMD                                'start ffmpeg with command strFFCMD string
        psiProcInfo.UseShellExecute = False                             'use the shell execute command we always want no
        psiProcInfo.WindowStyle = ProcessWindowStyle.Hidden             'hide the ffmpeg process window
        psiProcInfo.RedirectStandardError = True                        'Redirect the error out so we can read it
        psiProcInfo.RedirectStandardOutput = True                       'Redirect the standard out so we can read it
        psiProcInfo.CreateNoWindow = True                               'We dont create the ffmpeg window
        prcFFMPEG.StartInfo = psiProcInfo                               'ffmpeg process start information = all above informaton
        prcFFMPEG.Start() 'Start Process

        ffReader = prcFFMPEG.StandardError 'Enable Error Checking For FFMPEG.EXE


        Do       '' Bgw1 = Backgroundworker1
            If Bgw1.CancellationPending Then ' Have we pressed Cancel?
                Exit Sub

            End If

            Button5.Enabled = True      'Enable the cancel button
            Button3.Enabled = False     'Disable the convert button

            strFFOUT = ffReader.ReadLine        ' read each line from ffreader
            RichTextBox1.Text = strFFOUT        'Show each read line in the richtextbox

            'Get the progress from the current encoded frame
            If strFFOUT.Contains("frame=") Then         'if the strFFOut contains the string
                currentFramestr = Mid(strFFOUT, 7, 6)   'grab the next part after the string 'frame='
                currentFrameInt = CInt(currentFramestr) 'the next part is a string so convert the numbers back to an integer
            End If

            'Calculate the percentage of the progressbar
            Dim percentage As String = CType((ProgressBar1.Value / ProgressBar1.Maximum * 100), Integer).ToString & "%"

            ' report progress
            ProgressBar1.Maximum = FCount + 1000    'MediaInfo does not always get a accurate frame count so always add 1000 on top
            ProgressBar1.Value = (currentFrameInt)  'the current value is the current encoded frame
            Label12.Text = "Current Encoded Frame: " & currentFrameInt          'show the current frame in a label
            Label11.Text = percentage               'show the perecentage in a label
            'Label12.Text = ProgressBar1.Maximum 'for testing purposes


            ' Loop untill done
        Loop Until prcFFMPEG.HasExited And strFFOUT = Nothing Or strFFOUT = ""

        If ProgressBar1.Value <> ProgressBar1.Maximum Then
            ProgressBar1.Value = ProgressBar1.Maximum  'make the progressbar get to it's maximum on completion
        End If


        MsgBox("The Conversion Process has Completed.", MsgBoxStyle.Information, "Finished.")   'display on completion
        Button5.Enabled = False     'Disable the cancel button
        Button3.Enabled = True      'Reenable the convert button
        ProgressBar1.Value = 0      'reset the progressbar value to 0
        Label11.Text = ProgressBar1.Value & "%"     'get the progressbar value and report it as percent for visual purposes
        Label12.Text = "Current Encoded Frame: "
        If Me.Height = 508 Then     'if the show detailed information button was pressed reset the form height back to normal
            Me.Height = 441
            RichTextBox1.Visible = False    'Disable the richtextbox
        End If

    End Sub

In the bottom you can see the line: 在底部,您可以看到以下行:

Loop Until prcFFMPEG.HasExited And strFFOUT = Nothing Or strFFOUT = ""

But it's not clear where the loop start and end. 但是不清楚循环在哪里开始和结束。

...
ffReader = prcFFMPEG.StandardError 'Enable Error Checking For FFMPEG.EXE


**Do**       '' Bgw1 = Backgroundworker1
    If Bgw1.CancellationPending Then ' Have we pressed Cancel?
        Exit Sub

    End If
...

The Do is the start. 是开始。 Loop Until is the end. 循环直到结束。

This is your loop in C#. 这是您在C#中的循环。 in VB it starts with the Do and ends with the Loop Until [Condition] . 在VB中,它以Do开始,并以Loop Until [Condition]

do { //' Bgw1 = Backgroundworker1
    // Have we pressed Cancel?
    if (Bgw1.CancellationPending) {
        return;
    }

    //Enable the cancel button
    Button5.Enabled = true;
    //Disable the convert button
    Button3.Enabled = false;

    // read each line from ffreader
    strFFOUT = ffReader.ReadLine();
    //Show each read line in the richtextbox
    RichTextBox1.Text = strFFOUT;

    //Get the progress from the current encoded frame
    //if the strFFOut contains the string
    if (strFFOUT.Contains("frame=")) {
        //grab the next part after the string 'frame='
        currentFramestr = String.Mid(strFFOUT, 7, 6);
        //the next part is a string so convert the numbers back to an integer
        currentFrameInt = Convert.ToInt32(currentFramestr);
    }

    //Calculate the percentage of the progressbar
    string percentage = Convert.ToInt32((ProgressBar1.Value / ProgressBar1.Maximum * 100)).ToString + "%";

    // report progress
    //MediaInfo does not always get a accurate frame count so always add 1000 on top
    ProgressBar1.Maximum = FCount + 1000;
    //the current value is the current encoded frame
    ProgressBar1.Value = (currentFrameInt);
    //show the current frame in a label
    Label12.Text = "Current Encoded Frame: " + currentFrameInt;
    //show the perecentage in a label
    Label11.Text = percentage;
    //Label12.Text = ProgressBar1.Maximum 'for testing purposes

    // Loop until done
} while (!(prcFFMPEG.HasExited || string.IsNullOrEmpty(strFFOUT)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM