gpt4 book ai didi

.net - 线程池使用多达 200 个线程

转载 作者:行者123 更新时间:2023-12-04 06:21:32 24 4
gpt4 key购买 nike

我有这个代码,我用排队 10,000 运行,即使设置了最大 50 但线程池计数很高。

谁能帮我找出错误

Public Sub DoWork(ByVal objItem As Object)
Dim objUrl as String = DirectCast(objItem, string)
Try
If objUrl Is Nothing Then
exit sub
End If

Dim URL as New Uri(objUrl)

AddItem(URL.AbsoluteUri,gethtml(URL))

Catch ex As Exception
debug.Print("Error " & ex.StackTrace )
End Try

End Sub

Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
btnStart.Enabled =False
ThreadPool.SetMaxThreads(50, 50)
System.Threading.Thread.Sleep(1000)
listView1.Items.Clear

For Each sItem As String In txturls.Lines
if sItem.Trim <>string.empty

If sItem.Contains("http://")=False Then
sItem="http://" & sItem
End If

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf DoWork), sItem)
TotalItems+=1
tsslTotal.Text =String.Format ("Total Tasks: {0}",TotalItems)

end if
Next

End Sub

谢谢

编辑

对于那些问,

GetHtml 从网上获取网页源码

如果 GetHtml 到 Listview,则 AddItem 添加结果

再次感谢您的关注

编辑

我使用此代码检查每秒运行的线程数
Sub Timer1Tick(sender As Object, e As EventArgs)
tsslthreads.Text=string.Format ("Threads: {0}", Process.GetCurrentProcess().Threads.Count)
End Sub

谢谢

最佳答案

刚刚写了一个小演示项目。试试这个代码来检查 可用 线程,你会看到,只有 50 个线程 正在运行 .比较:您的代码将 队列 10000 个线程

private void timer1_Tick(object sender, EventArgs e)
{
int workerThreads = 0;
int completionPortThreads = 0;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);

lblTotal.Text = string.Format("Worker {0}/Completion {1}", workerThreads, completionPortThreads);
}

我的代码是在 c# 中,我添加了一个计时器来显示线程数..

关于.net - 线程池使用多达 200 个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6486422/

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