简体   繁体   English

VB.Net多线程-事件未触发?

[英]VB.Net Multithreading - Event Not Firing?

I've got this code, and let's say I am iterating over 100 items. 我有这段代码,可以说我要遍历100多个项目。 The first time I iterate over the list, the ProxyTested event fires 100 times. 我第一次遍历该列表时,ProxyTested事件将触发100次。 Great, that was easy. 太好了,那很容易。

Now comes the annoying unknown bug currently giving me a headache, if only it would give me an exception too, then I could probably figure out whats going wrong much easier. 现在出现了一个令人烦恼的未知错误,该错误目前使我头疼,如果只是也会给我例外,那么我可能会更容易找出问题所在。

Here it is: The second time I iterate over the list, and each time after, the ProxyTested event fires 99 times. 这是:第二次我遍历列表,此后每次ProxyTested事件都会触发99次。 What happened to 1 of my iterations? 我的迭代1中发生了什么?

PS For my project, Tasks throughput is much slower than spawning say 500 of my own threads, so because of that reason I don't want to use them. PS对于我的项目,“任务”吞吐量比生成自己的500个线程要慢得多,因此由于这个原因,我不想使用它们。 Just saying that before the inevitable suggestion. 只是说不可避免的建议之前。 It may not look so pretty, but it runs very, very fast compared to creating a list of Tasks and passing them into Task.WaitAll, which is admittedly much easier on the eyes. 它看起来可能不太漂亮,但是与创建Tasks列表并将它们传递给Task.WaitAll相比,它的运行速度非常快。

PSS Also, before the use a Concurrent Collection suggestion that is also inevitable, need to use a DataTable here, because I am also using SqliteDataAdapter.Update in this project, which only takes a DataTable or array of DataRow's. PSS另外,在使用不可避免的并发集合建议之前,需要在此处使用DataTable,因为在此项目中我还使用了SqliteDataAdapter.Update,它仅使用DataTable或DataRow的数组。

Dim counter as integer = 0
For i = 0 To ThreadCount - 1
            Dim t As New Thread(Sub()
                                Do

                                    Dim item As DataRow = Nothing
                                    Dim req As HttpWebRequest = Nothing
                                    SyncLock lock
                                        If counter > Proxies.Rows.Count Then
                                            Exit Do
                                        End If
                                        item = Proxies(counter)
                                        Interlocked.Increment(counter)
                                        Dim judge As DataRow = validJudges(rnd.Next(0, validJudges.Count))
                                        Dim proxy As String = item("ip") & ":" & item("port")

                                        req = HttpWebRequest.Create(judge("url"))
                                        req.Proxy = New WebProxy(proxy)
                                        req.Timeout = Timeout
                                        Debug.WriteLine(counter)
                                    End SyncLock

                                    Dim statusText As String = ProxyTest.GetStatus(req, "<title>AZ Environment variables 1.04</title>", externalIp)

                                    SyncLock lock
                                        item("statusText") = statusText

                                        If statusText = "Valid" Then
                                            Interlocked.Increment(ValidCount)
                                        Else
                                            Interlocked.Increment(InvalidCount)
                                        End If
                                        Interlocked.Increment(CompletedCount)
                                        RaiseEvent ProxyTested(item)
                                    End SyncLock

                                Loop
                                mre.Set()
                            End Sub)
            t.Start()
        Next
        mre.Reset()
        mre.WaitOne()

Re initializing my ManualResetEvent each time I called this method solved my problem :) 每当我调用此方法来解决我的问题时,就重新初始化ManualResetEvent :)

mre = New ManualResetEvent(False) mre =新的ManualResetEvent(False)

Previously mre was a global that was only initialized once during loading. 以前,mre是一个全局变量,在加载期间仅初始化一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM