gpt4 book ai didi

vb.net - WaitOne()函数的正确用法是什么

转载 作者:行者123 更新时间:2023-12-03 13:20:30 25 4
gpt4 key购买 nike

我尝试了一些线程池示例。我从Fibonacci example on MSDN web site开始,但是从this wasn't suitable for more than 64 calculations开始,所以我用以下代码解决了:

Imports System.Threading

Module Module1
Public Class Fibonacci
Private _n As Integer
Private _fibOfN
Private _doneEvent As ManualResetEvent

Public ReadOnly Property N() As Integer
Get
Return _n
End Get
End Property

Public ReadOnly Property FibOfN() As Integer
Get
Return _fibOfN
End Get
End Property

Sub New(ByVal n As Integer, ByVal doneEvent As ManualResetEvent)
_n = n
_doneEvent = doneEvent
End Sub

' Wrapper method for use with the thread pool.
Public Sub ThreadPoolCallBackMar(ByVal threadContext As Object)
Dim threadIndex As Integer = CType(threadContext, Integer)
Console.WriteLine("thread {0} started...", threadIndex)
_fibOfN = Calculate(_n)
Console.WriteLine("thread {0} result calculated...", threadIndex)
_doneEvent.Set()
End Sub

Public Function Calculate(ByVal n As Integer) As Integer
If n <= 1 Then
Return n
End If
Return Calculate(n - 1) + Calculate(n - 2)
End Function

End Class


<MTAThread()>
Sub Main()
Const FibonacciCalculations As Integer = 65

' One event is used for each Fibonacci object
Dim doneEvents(FibonacciCalculations) As ManualResetEvent
Dim fibArray(FibonacciCalculations) As Fibonacci
Dim r As New Random()

' Configure and start threads using ThreadPool.
Console.WriteLine("launching {0} tasks...", FibonacciCalculations)

For i As Integer = 0 To FibonacciCalculations
doneEvents(i) = New ManualResetEvent(False)
Dim f = New Fibonacci(r.Next(20, 40), doneEvents(i))
fibArray(i) = f
ThreadPool.QueueUserWorkItem(AddressOf f.ThreadPoolCallBackMar, i)
Next

Console.WriteLine("All calculations are complete.")

For i As Integer = 0 To FibonacciCalculations
doneEvents(i).WaitOne()
Dim f As Fibonacci = fibArray(i)
Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN)
Next

Console.Read()
End Sub
End Module

使用 WaitOne()而不是 WaitAll()解决了问题,但问题是:如果我不需要显示结果,那么我就不需要第二个循环,但是...无需在第二个循环中放置 waitOne()函数?

最佳答案

您的代码基本上是这样做的:

// start a bunch of threads to do calculations

Console.WriteLine("All calculations are complete."); // This is a lie!

// Wait for the threads to exit

这里的主要问题是,对 Console.WriteLine进行调用时,计算尚未完成。好吧,它们可能是完整的,但是除非您等到事件显示信号,否则您不知道。
WaitOne的目的是告诉您计算是否完成。您的代码应这样写:
    For i As Integer = 0 To FibonacciCalculations
doneEvents(i) = New ManualResetEvent(False)
Dim f = New Fibonacci(r.Next(20, 40), doneEvents(i))
fibArray(i) = f
ThreadPool.QueueUserWorkItem(AddressOf f.ThreadPoolCallBackMar, i)
Next

Console.WriteLine("All calculations are started. Waiting for them to complete.")

For i As Integer = 0 To FibonacciCalculations
doneEvents(i).WaitOne()
Dim f As Fibonacci = fibArray(i)
Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN)
Next

Console.WriteLine("All calculations are complete.")

您必须检查事件以了解计算已完成。

现在,如果您不需要知道计算是否完成,则完全不需要 WaitOne。而且,如果您不打算等待该事件,那么就没有真正需要该事件了,对吗?尽管有人想知道为什么要进行计算然后不使用结果。

关于vb.net - WaitOne()函数的正确用法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18126979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com