In WPF implementation
We often need to achieve this goal: a thread that performs complex tasks, and then the main form waits for animation
I wrapped my simplest things, more convenient to use, we can also be convenient to use
1: Add Commhelper class Fasttask method
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 {Publi c static void Fasttask (action _actiondoing, action _actionafter=null) {Processcontrol loading = new Pro Cesscontrol (); Task T = new Task (() = {try {_actiondoing (); Application.Current.Dispatcher.BeginInvoke (new Action () = {Loadin G.close (); })); if (_actionafter! = null) {_actionafter ();} } catch (Exception ex) {Application.Current.Dispatcher.Invoke (new Action (() = {loading. Close (); MessageBox.Show ("failed \ r \ n" + EX.tostring ());//new Popupwindow (). Showthis ("Drawing failed"); })); } }); T.start (); Loading. ShowDialog (); ; } }}
Fasttask (Action _actiondoing, action _actionafter = null)
_actiondoing is waiting to do things,
_actionafter is the thing to do after waiting to be done, if cross-threading, consider calling across threads
2: Add an animated DLL
3: Start Testing
private void Button_Click (object sender, RoutedEventArgs e) { commhelper.fasttask (new Action () = { Thread.Sleep (1000);//dry complex things }), new Action (() = { MessageBox.Show ("Load Complete"); }); }
, easy to use in the future!
WPF Custom Fast Implementation thread wait function Fasttask