0

In my app after login success, I need to start a thread which connecting to a wcf service for sync data between the app and a backend web. This process need to carry on every 1 minute and I should be able to start and stop this service from ui as well. Please advice me a proper way of doing it.

Currently what I've done was adding a async void method and inside that method I'm running a while(isRunning) loop. So from the Ui I can accessing the "isRunning" variable and make it false to stop the process. This is not entirely accurate because if the method is in middle of a http request with the WCF service, it will not interrupt.

I also can't use background worker because sync function on the WCF service return pdf files (as data) and those were come as byte[]

My objective is to run this polling method keeping the UI responsiveness and impactless to the entire app.

If you want further clarification please ask.

svick
  • 236,525
  • 50
  • 385
  • 514
SurenSaluka
  • 1,534
  • 3
  • 18
  • 36
  • Look into DispatchTimer to replace your while loop. The timer can be stopped and restarted and the interval is configurable. The Tick event handler can call your WCF call to update the data. – mdebeus May 31 '15 at 13:13
  • Thanks, but even if the timer stopped, will that guarantee http requests also going to stop? Because if the http request took 1 minute to finish, it will not even see the timer has stopped way before. – SurenSaluka May 31 '15 at 15:03
  • 1
    The tick method should stop the timer, call your async wcf method, and in the completed handler, restart the timer. It should also contain a try/catch in case an exception is thrown and the completed handler is never called. In the catch you should restart the timer. Was your question really asking how to stop a wcf call once it has already started? – mdebeus May 31 '15 at 16:40
  • yes, i want stop the wcf call as well. Actually there are 3 simultaneous calls. I want to stop all 3. Thnaks. – SurenSaluka Jun 01 '15 at 06:08
  • FYI - that is an entirely different question than the one stated above. Not sure you can cancel the requests. Here are some links that might help: http://stackoverflow.com/questions/949588/how-to-cancel-wcf-service-call, http://stackoverflow.com/questions/15324995/cancel-a-long-running-task-over-wcf-from-client, http://stackoverflow.com/questions/3039323/whats-the-best-way-to-cancel-an-asynchronous-wcf-request – mdebeus Jun 01 '15 at 17:54
  • Hi again, about the dispatcher timer! assume – SurenSaluka Jun 03 '15 at 13:10

0 Answers0