I get a connection list sometime 70 people in the list sometime 1000 , i put a loop on it and start getting them one by one using HTTPWEBREQUEST, and i have a textbox that keeps showing me where i am at , 10-15 or 200 etc,,
My problem is i have a stop button on this, and when i press stop button i want it to break out of it, how can i achieve that?
I am not clear what to do when i press stop? Because the form(its a windows form application) gets stuck/hanged until it is completed…
Any help is appreciated ,
Thank you in advance.
It sounds like you’re doing all the work in the UI thread. Instead, you should either use asynchronous web requests (possibly using
WebClient) or do the downloading in a worker thread (e.g. usingBackgroundWorker). That way your UI will stay responsive, and you can have some sort of shared flag to indicate whether or not you should keep processing the list. Note that you’ll want to either usevolatile, locking, or theInterlockedclass to make sure that changes made to the shared flag in the UI thread are visible in the worker thread.Another alternative is to use the Task Parallel Library (TPL) from .NET 4, with a cancellation token. That’s a bit more advanced, but would be cleaner in the long run.
Note that C# 5 will make asynchrony significantly easier.