Vb.net 流程执行期间不显示表单内容

Vb.net 流程执行期间不显示表单内容,vb.net,graphics,process,Vb.net,Graphics,Process,我有一个主窗体FormMain,它在单击按钮时执行一个进程。 单击按钮时,首先显示一个无边框表单StatusForm,然后流程开始。请参阅下面的代码 Public Class FormMain Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ToolStripStatusLabel1.Text = "Compiling..." ToolStr

我有一个主窗体
FormMain
,它在单击按钮时执行一个进程。
单击按钮时,首先显示一个无边框表单
StatusForm
,然后流程开始。请参阅下面的代码

Public Class FormMain

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ToolStripStatusLabel1.Text = "Compiling..."
    ToolStripStatusLabel1.ForeColor = Color.White
    StatusStrip1.BackColor = Color.CadetBlue
    StatusForm.TopMost = True
    StatusForm.Show()

    RichTextBoxEditor.Text = My.Computer.FileSystem.ReadAllText("C:\Users\Swaroop\Documents\temp\test.cpp")
    proc.CreateNoWindow = True
    proc.UseShellExecute = False
    proc.RedirectStandardInput = True
    proc.RedirectStandardOutput = True
    'proc.RedirectStandardError = True
    pr = Process.Start(proc)
    pr.StandardInput.WriteLine("echo off")
    pr.StandardInput.WriteLine("g++ ""C:\Users\Swaroop\Documents\temp\test.cpp""" & " -o " & """C:\Users\Swaroop\Documents\temp\test""")
    'pr.StandardInput.WriteLine(FileType & " " & """" & FileName & """" & " -o " & """" & Filepath & "\" & FileNameNoExt & """")
    pr.StandardInput.Close()
    RichTextBoxConsole.AppendText(pr.StandardOutput.ReadToEnd())
    proc.RedirectStandardError = True
    RichTextBoxConsole.AppendText(pr.StandardError.ReadToEnd())

    If pr.StandardOutput.EndOfStream Then
        StatusForm.Close()
        compilationStatus()
        pr.StandardOutput.Close()
        pr.StandardError.Close()
    End If

End Sub

Private Sub compilationStatus()
    If RichTextBoxConsole.Text.Contains("error") Then
        Dim MSplit As Array = Split(RichTextBoxConsole.Text, "error")
        Dim countWordError As Integer = MSplit.Length - 1
        ToolStripStatusLabel1.Text = "Compilation failed with (" & countWordError.ToString & ") errors."
        StatusStrip1.BackColor = Color.Red
        ToolStripStatusLabel1.ForeColor = Color.White

    ElseIf RichTextBoxConsole.Text.Contains("warning") Then
        Dim MSplit As Array = Split(RichTextBoxConsole.Text, "warning")
        Dim countWordWarning As Integer = MSplit.Length - 1
        ToolStripStatusLabel1.Text = "Compilation succeeded with (" & countWordWarning.ToString & ") warnings."
        StatusStrip1.BackColor = Color.Yellow
        ToolStripStatusLabel1.ForeColor = Color.White
    Else
        ToolStripStatusLabel1.Text = "Compilation succeeded."
        StatusStrip1.BackColor = Color.Blue
        ToolStripStatusLabel1.ForeColor = Color.White
    End If

End Sub
End Class

Public Class StatusForm

Private Sub StatusForm_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    Dim g As Graphics
    g = e.Graphics
    Dim p As Pen = New Pen(Color.Gray, 8)
    g.DrawRectangle(p, e.ClipRectangle)
    g.DrawString("Compiling...", New Font("Arial", 16), Brushes.Gray, 35, 37)
End Sub

End Class

问题在于,当点击按钮时显示表单
StatusForm
,该表单将显示,而不创建
StatusForm\u Paint
子部分中所述的图形。我的意思是显示空白表单。我还尝试删除
DrawString
方法,并在表单中放置了一个label控件。单击按钮时,会显示相同的空白表单,标签的边界区域变得透明和透明。

这是因为调用
Show
后的所有操作都在UI线程上完成,因此线程无法绘制其他表单。一个线程一次只能做一件事。有许多特定选项可用,包括调用异步方法,例如
StreamReader.ReadToEndAsync
。不过,这个问题不仅仅是更改一两行代码。这是关于你理解多线程和并行性的原理。您应该阅读这些主题。同时,您可能会从使用中受益。这是因为调用
Show
后的所有操作都是在UI线程上完成的,因此该线程无法绘制其他表单。一个线程一次只能做一件事。有许多特定选项可用,包括调用异步方法,例如
StreamReader.ReadToEndAsync
。不过,这个问题不仅仅是更改一两行代码。这是关于你理解多线程和并行性的原理。你应该读一些关于这些主题的书。同时,你可能会从中受益。