Why does this Parallel.ForEach code freeze the program up?

后端 未结 6 1655
南笙
南笙 2020-11-30 13:37

More newbie questions:

This code grabs a number of proxies from the list in the main window (I couldn\'t figure out how to make variables be available between diffe

6条回答
  •  北海茫月
    2020-11-30 14:18

    One problem with your code is that you're calling FinishedProxies.Add from multiple threads concurrently. That's going to cause a problem because List isn't thread-safe. You'll need to protect it with a lock or some other synchronization primitive, or use a concurrent collection.

    Whether that causes the UI lockup, I don't know. Without more information, it's hard to say. If the proxies list is very long and checkProxy doesn't take long to execute, then your tasks will all queue up behind that Invoke call. That's going to cause a whole bunch of pending UI updates. That will lock up the UI because the UI thread is busy servicing those queued requests.

提交回复
热议问题