I am developing a WPF
client application.This app sends data periodically to the webservice
. When user logged into the app I want run particular method every 5 mts to send data to the .asmx
service.
My question is whether I need to use threading or timer.This method execution should happen while user is interacting with the application. i.e without blocking the UI during this method execution
Any resources to look for ?
I would recommend the
System.Threading.Tasks
namespace using the newasync/await
keywords.Then you would just call this method somewhere:
If you want the method to execute on a different thread than the UI one, use
System.Threading.Timer
. Otherwise (but I don't think it's your case), useSystem.Windows.Threading.DispatcherTimer
.You need to use
Timer
class. There are multiple built-in timers and it depends on the requirement which timer to use.System.Timers.Timer: This is more suitable for mutlithreaded access. Instances of this timer are threadsafe.
System.Threading.Timer : Instances of this timer are not thread safe.
System.Windows.Threading.DispatcherTimer -> It sends event to
Dispatcher
thread (and is not multithreaded). This is useful if you need to update UI.System.Windows.Forms.Timer -> This timer raises events in UI thread. This is optimized for windows forms, and not to be used in WPF.
Following is an interesting read.
Comparing the Timer Classes in the .NET Framework Class Library