VB.NET: How to maintain main form control during loops in other sub?

偶尔善良 提交于 2019-12-13 00:49:11

问题


I have a program with only one form. By pressing a button it starts a ffmpeg conversion.

In the main form, in textboxes, ffmpeg stats are outputted. This is possible by taking StandardError output from ffmpeg.

Public Sub Console()
        Dim Process As New Process 

        Process.StartInfo.UseShellExecute = False
        Process.StartInfo.RedirectStandardError = True
        Process.StartInfo.RedirectStandardOutput = True 
        Process.StartInfo.FileName = current_ffmpeg_path 
        Process.StartInfo.Arguments = input_params 
        Process.StartInfo.CreateNoWindow = True 
        Process.Start() 

        Dim ffmpeg_stats_output As System.IO.StreamReader = Process.StandardError

        Do While Process.HasExited = False
        [update all main form textboxes by taking input string from ffmpeg and elaborate it]
        Loop
End Sub

The problem is that while this loop is executed the textboxes and progress bar are updated but the main form cannot be modified. There is in fact no control at all by user. So if I want to make a button to stop/pause ffmpeg in main form this cannot be pressed as anything else on the main form.

there is a way to maintain loops inside other Sub without loose control of main form while they're running?

I tried to fix it by calling another dialog form with textboxes and progress bar. But even this form loose completely control until the process is finished.

To send pause/stop conversion to ffmpeg (that runs without a window) is it correct use:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SendKeys.Send("q")
SendKeys.Send("^s") 
End Sub

or must be specified that this key is sent to the current running Process?


回答1:


solved the problem with no control on main window...

just use a BackgroundWorker.



来源:https://stackoverflow.com/questions/28265331/vb-net-how-to-maintain-main-form-control-during-loops-in-other-sub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!