WPF Custom Fast Implementation thread wait function Fasttask

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.