如何退出/中止/停止c#WpF MVVM中的函数 [英] How to exit/abort/stop a function in c# WpF MVVM
本文介绍了如何退出/中止/停止c#WpF MVVM中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// 对于开始按钮
public void MyAction1()
{
// 代码在这里
}
// 对于退出按钮功能
public void MyExit1()
{
Thread thrd1 = new Thread(()= > MyAction1());
thrd1.Abort();
}
如何使用Thread停止MyAction1()函数。
解决方案
由于你正在使用WPF,我建议使用任务而不是直接启动新线程。看一下任务类 [ ^ ]例如使用任务并行库WPF中的(TPL) [ ^ ]或任务并行库: 6 of n [ ^ ]
// For Start Button
public void MyAction1()
{
// Code Here
}
// For Exit Button function
public void MyExit1()
{
Thread thrd1 = new Thread(() => MyAction1());
thrd1.Abort();
}
How should i Stop MyAction1() Function Using Thread.
解决方案
Since you're using WPF I would recommend using tasks instead of directly starting new threads. Have a look at Task class[^] and for example Using the Task Parallel Library (TPL) in WPF[^] or Task Parallel Library: 6 of n[^]
这篇关于如何退出/中止/停止c#WpF MVVM中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文