如何使用线程或计时器从 WPF 客户端应用程序定期执行方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14296644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute a method periodically from WPF client application using threading or timer
提问by umsateesh
I am developing a WPFclient 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 .asmxservice.
我正在开发一个WPF客户端应用程序webservice。这个应用程序会定期将数据发送到. 当用户登录应用程序时,我希望每 5 米运行一次特定方法以将数据发送到.asmx服务。
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
我的问题是我是否需要使用线程或计时器。在用户与应用程序交互时应该执行此方法。即在此方法执行期间不阻塞 UI
Any resources to look for ?
有什么资源要找吗?
回答by Erik
I would recommend the System.Threading.Tasksnamespace using the new async/awaitkeywords.
我会推荐System.Threading.Tasks使用 newasync/await关键字的命名空间。
// The `onTick` method will be called periodically unless cancelled.
private static async Task RunPeriodicAsync(Action onTick,
TimeSpan dueTime,
TimeSpan interval,
CancellationToken token)
{
// Initial wait time before we begin the periodic loop.
if(dueTime > TimeSpan.Zero)
await Task.Delay(dueTime, token);
// Repeat this loop until cancelled.
while(!token.IsCancellationRequested)
{
// Call our onTick function.
onTick?.Invoke();
// Wait to repeat again.
if(interval > TimeSpan.Zero)
await Task.Delay(interval, token);
}
}
Then you would just call this method somewhere:
然后你就可以在某处调用这个方法:
private void Initialize()
{
var dueTime = TimeSpan.FromSeconds(5);
var interval = TimeSpan.FromSeconds(5);
// TODO: Add a CancellationTokenSource and supply the token here instead of None.
RunPeriodicAsync(OnTick, dueTime, interval, CancellationToken.None);
}
private void OnTick()
{
// TODO: Your code here
}
回答by Tilak
You need to use Timerclass. There are multiple built-in timers and it depends on the requirement which timer to use.
你需要使用Timer类。有多个内置计时器,这取决于使用哪个计时器的要求。
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
Dispatcherthread (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.
System.Timers.Timer:这个更适合多线程访问。此计时器的实例是线程安全的。
System.Threading.Timer:此计时器的实例不是线程安全的。
System.Windows.Threading.DispatcherTimer-> 它向
Dispatcher线程发送事件(并且不是多线程的)。如果您需要更新 UI,这很有用。System.Windows.Forms.Timer-> 此计时器在 UI 线程中引发事件。这是为 windows 窗体优化的,不能在 WPF 中使用。
Following is an interesting read.
Comparing the Timer Classes in the .NET Framework Class Library
以下是一个有趣的阅读。
比较 .NET Framework 类库中的计时器类
回答by Mir
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), use System.Windows.Threading.DispatcherTimer.
如果您希望该方法在与 UI 线程不同的线程上执行,请使用System.Threading.Timer. 否则(但我不认为这是你的情况),使用System.Windows.Threading.DispatcherTimer.

