且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将相同的变量传递给多个线程

更新时间:2022-11-23 14:16:23

当你展示MessageBox时,有有足够的时间执行其他线程,直到您单击消息框。当您不显示消息框时,您将拥有一个新的线程队列,这些新线程将在您的循环结束后执行。所有这些新线程都收到了你的列表bldstr,它总是包含最后一个scrpurl值。因此,所有线程都处理最后一个值。

您可以删除对bldstr.Clear()的调用,并更改它接受字符串而不是字符串列表的setdata参数,并传递bldstr(i-1)到它。
When you show the MessageBox, there is enough time for the other thread to be executed till you clicked the messagebox away. When you do not show the messagebox, you''ll have a long queue of new threads which are executed after your loop ended. And all those new threads received your list "bldstr" which always contains the last scrpurl value. Consequently, all threads work on that last value.
You could remove the call to bldstr.Clear(), and change the parameters of setdata that it accepts a string instead of a list of strings, and pass bldstr(i-1) to it.