在WPF实现
我们常常需要实现这个目标:线程里面执行复杂的任务,然后主窗体等待动画
我把我最简单的东西给包了一下,更方便使用,大家也可以方便使用
1:添加CommHelper类 FastTask方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using LoadingIndicators.WPF;
using System.Threading.Tasks;
namespace PLC_Data_Backup
{
public class CommHelper
{
public static void FastTask(Action _actionDoing, Action _actionAfter=null)
{
ProcessControl loading = new ProcessControl();
Task t = new Task(() =>
{
try
{
_actionDoing();
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
loading.Close();
}));
if (_actionAfter != null)
{ _actionAfter(); }
}
catch (Exception ex)
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
loading.Close();
MessageBox.Show("失败\r\n" + ex.ToString());//new PopupWindow().ShowThis("绘图失败");
}));
}
});
t.Start();
loading.ShowDialog();
;
}
}
}
FastTask(Action _actionDoing, Action _actionAfter = null)
_actionDoing是等待中做的事情,
_actionAfter是等待做完之后做的事情,如果跨线程请考虑跨线程调用
2:添加动画 dll
![](https://i-blog.csdnimg.cn/blog_migrate/56f431a50e5af7d1767f27400a373acf.png)
3:开始测试
private void Button_Click(object sender, RoutedEventArgs e)
{
CommHelper.FastTask(new Action(() =>
{
Thread.Sleep(1000);//干复杂的事情
}),
new Action(()=>
{
MessageBox.Show("加载完成");
}));
}
,方便以后简单的使用了!