从函数在 ParameterizedThreadStart 中分配布尔值

Assiging boolean in a ParameterizedThreadStart from a function

我在使用 ParameterizedThreadStart 为布尔数据类型分配值时遇到问题,当它 returns 时它总是显示 False

这是我的代码:

'smth
Frm_ChkHash.Show()
Frm_ChkHash.BringToFront()

Dim BoolH As Boolean
Dim thdC = New Thread(New ParameterizedThreadStart(Function() BoolH = Frm_ChkHash.MatchHash(Keyl)))
thdC.Start() 
'smth (Wait the thread until exit)
Debug.WriteLine("It is " & BoolH)
'smth

Debug.WriteLine("It is " & BoolH)BoolH

中显示False

试图将其设置为可空 (Dim BoolH? As Boolean) 它在 BoolH

上什么也没显示

这是我的 Frm_ChkHash.MatchHash 函数代码:

Public Function MatchHash(ByVal Keyl As String) As Boolean

    Dim nameApp As String = dicLbl.Item(Keyl)
    Debug.WriteLine("Hey! I am checking " & nameApp)

    Thread.Sleep(1500)
    InitHsh()
    Thread.Sleep(2000)

    Dim GetHash As String = KHash.GenerateHash(pathFile, HashCheck.HashType.SHA1) 
    'The KHash.GenerateHash returns a String.
    Thread.Sleep(1500)

    'Find the Actual Hash in the Dictionary through the key.
    Dim ActualHash As String = dicHsh.Item(Keyl)
    Debug.WriteLine("The actual hash is: " & ActualHash)

    Dim StrCmp_Hash As Boolean = StringComparer.OrdinalIgnoreCase.Equals(ActualHash, GetHash)
    Debug.WriteLine("The hash is " & CStr(StrCmp_Hash))

    If StrCmp_Hash = True Then

        Debug.WriteLine("The hash is correct!")
        Debug.WriteLine("It is cool: " & dicHsh.Item(Keyl))
        Debug.WriteLine("And I get : " & GetHash)

        Thread.Sleep(1500)
        Hide()

        Return True

    Else

        Debug.WriteLine("I get" & GetHash & "But it is" & dicHsh.Item(Keyl))

        Thread.Sleep(1500)
        Hide()
        Return False

    End If

    Hide()

End Function

我的输出 window 显示如下:

Hey! I am checking ThisApp       <--- This comes from MatchHash function
The actual hash is: E2133C93F55C7DF4EA44DC0F5455F4A2EE637E8B
The hash is True
The hash is correct!
It is cool: E2133C93F55C7DF4EA44DC0F5455F4A2EE637E8B
And I get : E2133C93F55C7DF4EA44DC0F5455F4A2EE637E8B
The thread 0x8cc has exited with code 0 (0x0).  <--- IDK where this line comes form

It is       <--- The function had returned. ( After `thdC.start()` )

感谢任何帮助。

问题是您的委托是您的委托中的“=”运算符充当比较运算符而不是赋值运算符。

它的作用是这样的:

Function() 
    return BoolH = Frm_ChkHash.MatchHash(Keyl)
End If

这就是为什么 BoolH 可以为 null 时为 null 的原因。

如果要将 BoolH 赋值给值,请使用 'Sub()' 而不是 Function() 或使您的委托成为多行语句。

Dim thdC = New Thread(New ParameterizedThreadStart(Sub() BoolH = Frm_ChkHash.MatchHash(Keyl)))

或:

Dim thdC = New Thread(New ParameterizedThreadStart(Function() 
        BoolH = Frm_ChkHash.MatchHash(Keyl)
    End Function)

并不是说后者现在没有 return 价值。