HDL Home App 第二版本 旧平台金堂用 正在使用
hxb
2022-08-30 25429f085093d89d543a0b90e30d0d62d1b7dac9
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlThreadLogic.cs
New file
@@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 线程逻辑(创建这个东西,是为了Log出力)
    /// </summary>
    public class HdlThreadLogic
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 线程逻辑(创建这个东西,是为了Log出力)
        /// </summary>
        private static HdlThreadLogic m_Current = null;
        /// <summary>
        /// 线程逻辑(创建这个东西,是为了Log出力)
        /// </summary>
        public static HdlThreadLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlThreadLogic();
                }
                return m_Current;
            }
        }
        #endregion
        #region ■ 线程执行___________________________
        /// <summary>
        /// 子线程执行(创建这个东西,是为了Log出力)
        /// </summary>
        public void RunThread(Action action, ShowErrorMode mode = ShowErrorMode.YES)
        {
            new System.Threading.Thread(() =>
            {
                //记录起当前界面,虽然似乎没啥用
                string nowFormId = UserCenterResourse.NowActionFormID;
                try
                {
                    action.Invoke();
                    action = null;
                }
                catch (Exception ex)
                {
                    if (mode == ShowErrorMode.YES)
                    {
                        Application.RunOnMainThread(() =>
                        {
                            //出现未知错误,数据丢失
                            var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError));
                            alert.Show();
                            //出现异常,则关闭进度条
                            ProgressBar.Close();
                        });
                    }
                    //Log出力
                    string msg = "当前激活的界面[" + nowFormId + "]";
                    HdlLogLogic.Current.WriteLog(ex, msg);
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 执行运行子线程里面的主线程(创建这个东西,是为了Log出力)
        /// </summary>
        public void RunMainInThread(Action action, ShowErrorMode mode = ShowErrorMode.YES)
        {
            new System.Threading.Thread(() =>
            {
                Application.RunOnMainThread(() =>
                {
                    //记录起当前界面,虽然似乎没啥用
                    string nowFormId = UserCenterResourse.NowActionFormID;
                    try
                    {
                        action.Invoke();
                        action = null;
                    }
                    catch (Exception ex)
                    {
                        if (mode == ShowErrorMode.YES)
                        {
                            //出现未知错误,数据丢失
                            var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError));
                            alert.Show();
                        }
                        //Log出力
                        string msg = "当前激活的界面[" + nowFormId + "]";
                        HdlLogLogic.Current.WriteLog(ex, msg);
                        //出现异常,则关闭进度条
                        ProgressBar.Close();
                    }
                });
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 执行运行于主线程(创建这个东西,是为了Log出力)
        /// </summary>
        public void RunMain(Action action, ShowErrorMode mode = ShowErrorMode.YES)
        {
            Application.RunOnMainThread(() =>
            {
                //记录起当前界面,虽然似乎没啥用
                string nowFormId = UserCenterResourse.NowActionFormID;
                try
                {
                    action.Invoke();
                    action = null;
                }
                catch (Exception ex)
                {
                    if (mode == ShowErrorMode.YES)
                    {
                        //出现未知错误,数据丢失
                        var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError));
                        alert.Show();
                    }
                    //Log出力
                    string msg = "当前激活的界面[" + nowFormId + "]";
                    HdlLogLogic.Current.WriteLog(ex, msg);
                    //出现异常,则关闭进度条
                    ProgressBar.Close();
                }
            });
        }
        #endregion
    }
}