A small way to troubleshoot access to UI exceptions in other threads

Source: Internet
Author: User

Children's shoes that write WPF may encounter problems accessing UI exceptions in child threads. This is a security restriction to prevent data inconsistencies.

It is troublesome to update the UI in a child thread and give the main thread an update.

Next, we introduce a solution that can be called a framework (tap the brick when it's tapped).

One: Solve the problem of judging the current thread is the main line

In C #, Microsoft does not seem to give a direct judge of whether the current thread is the main path of the scheme, at least I did not find.

/// <summary>///Lyx Threading Framework class/// </summary> Public classlyxthreadframe{/// <summary>    ///Main thread Signature/// </summary>     Public Const stringMainthreadidiograph ="Main Thread"; /// <summary>    ///initializing the thread detection framework/// <para>Initialize under the UI (Master) thread</para>    /// </summary>     Public Static voidInit () {varThread =System.Threading.Thread.CurrentThread; Thread. Name=mainthreadidiograph; }}

My solution is to name the main thread first when the program starts

 Public Partial class app:application{    protectedoverridevoid  onstartup (StartupEventArgs e)    {        lyxthreadframe.init ();         Base . Onstartup (e);    }}

I'll rewrite the Onstartup () method of the application class to initialize the current frame in this method

Onstartup () The entry point that the WPF program started, where the main window was created.

/// <summary>///Thread Extension Classes/// </summary> Public Static classthreadextension{/// <summary>    ///whether the current thread is the main path/// </summary>     Public Static BOOLIsmainthread ( Thisthread thread) {        if(Thread = =NULL)        {            Throw NewArgumentNullException ("Thread"); }        if(Thread. Name = =NULL)        {            return false; }        returnthread.    Name.equals (lyxthreadframe.mainthreadidiograph); }}

The extended thread class is used to determine whether the current method is the main thread and whether the specified thread is the one that the brother gave the name to.

Saying that this extended class method is really a good idea.

Well, here you can tell if the current thread is the main path.

Two: Let your UI access code execute under the UI (Master) thread
/// <summary>///Delegate Extension Classes/// </summary> Public Static classdelegateextension{/// <summary>    ///executing in the UI (master) thread/// </summary>     Public Static ObjectSafetyinvoke ( ThisDelegate Dele,params Object[] param) {        varThread =System.Threading.Thread.CurrentThread; if(thread. Ismainthread ()) {returnDele.        DynamicInvoke (param); }        Else        {            returnApplication.Current.Dispatcher.Invoke (dele, param); }    }}

The Delegate class under. Net is the identity of Lao Tzu, and all the delegates inherit to it. Here we extend the Delegate

Here is a application class, the app in our program is the foundation of this. It is the entry point of the program.

This allows us to create a secure environment for accessing the UI

Example:

 Public void Hello () {    new Action (() + =        {// update UI    });
Action. Safetyinvoke ();}

A small way to troubleshoot access to UI exceptions in other threads

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.