VB.NET阻止,直到用户以子格式输入文本?

时间:2017-10-21 11:00:14

标签: .net vb.net user-input wait blocking

我正在以编程方式创建第二个自定义表单,并尝试在调用线程上暂停执行,直到在子表单中输入所需的值。我当前的实现阻止了我在子表单中输入所需值的能力。当然,使用代码示例更有意义,所以这里:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Task.Run(Sub()
                 Dim AnswerSolverForm As New AnswerSolvingForm
                 Dim answer As New Answer
                 AnswerSolverForm.AddAnswer(answer)
                 AnswerSolverForm.Show()
                 answer.WaitUntilSolved()
                 Debug.WriteLine(answer.Value)
             End Sub)
End Sub

Public Class AnswerSolvingForm

    Private _answers As New List(Of Answer)

    Public ReadOnly Property Answers As List(Of Answer)
        Get
            Return _answers
        End Get
    End Property

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        _answers.Item(0).Value = txtAnswer.Text
        _answers.RemoveAt(0)
    End Sub

    Public Sub AddAnswer(answer As Answer)
        _answers.Add(answer)
    End Sub

End Class

Public Class Answer

    Private _mre As Threading.ManualResetEvent
    Private _value As String

    Public Property Value As String
        Get
            Return _value
        End Get
        Set(value As String)
            _value = value
            _mre.Set()
        End Set
    End Property

    Public Sub WaitUntilSolved()
        _mre = New Threading.ManualResetEvent(False)
        _mre.WaitOne()
    End Sub

End Class

0 个答案:

没有答案