0

i'm writing some kind of application that will send http requests to server. My app will send requests with some interval by timer. for example, i need to send 1 type of requests with time interval 2 seconds, 2nd type of requests - 10 seconds and so on. i understand that it is better to have one scheduler that will push my requests. but the question is how can i send requests and process responses without freezing ui?

p.s. right now i have class MyAdapter, which encapsulates all work with requests and responses, and class MyScheduler, which sends requests by timer.

user1178399
  • 1,028
  • 8
  • 17
  • 32

3 Answers3

1

You should indeed then use a BackGroundWorker and if you need to update the UI in the meantime, you can use the Dispatcher.Invoke() method.

Abbas
  • 14,186
  • 6
  • 41
  • 72
  • 1
    I agree completely with @Abbas, my only comment to his/her suggestion would be that Dispatcher.Invoke() is available if your application is a WPF app; if you're under Windows Forms you would use `Control.Invoke()` to safely interact with the user interface from your BackgroundWorker process. I recently responded to a similar question here: [link](http://stackoverflow.com/questions/9370655/allowing-user-interaction/9371623#9371623). You can see there an example of using `Control.Invoke()`. –  Feb 21 '12 at 15:12
1

but the question is how can i send requests and process responses without freezing ui?

You could use timers to send your requests at regular intervals. If you need to update your UI when the request succeeds you might need to use the Dispatcher.Invoke or Control.Invoke.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

You have a variety of options for performing work in the background while the UI stays responsive:

  • Old, but reliable: BackgroundWorker has been around the .net Framework for a long time and would work perfectly fine for your requirements. Lots of examples can be found on this site and elsewhere.

  • State of the art: The Task class, available since .NET 4, is considered to be an improvement over BackgroundWorker.

  • Brand new: The Async extensions are the new way to do "background stuff". No need for multi-threading and manual synchronization, everything "just works". However, this is just at "community preview" stage at the moment.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519