开发者

When querying a large number of remote network PCs how can I time out in a foreach loop?

I'm using a foreach loop to query remote PCs, for some reason querying a certain PCs the program sticks on the PC (maybe it's been turned off mid query), how can I wait for awhile (although a fixed length of time would be no good as some can take minutes to query others 30 mins) and then move on to the next PC?

foreach (String pc in pcs)
            {
                i开发者_JAVA技巧f (bg_worker.CancellationPending) break; 
               ...
} 

Thanks

Jade


Use a threadpool to query for several hosts simultaneously. This does three things for you:

  1. Dramatically reduce your total scan time, since currently your program is likely spending a lot of time waiting for responses.
  2. Allow you to use the built-in mechanisms to terminate asynchronous threads to implement your timeout
  3. Means a "stuck" PC won't stop all scanning while waiting to timeout - just that thread


See http://www.codeplex.com/smartthreadpool It supports work item cancelation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜