Winform跨线程打印日志到TextBox(使用委托跨线程访问控件)

来源:互联网 发布:蓝桥杯全国软件大赛 编辑:程序博客网 时间:2024/06/03 21:53
class Test    {        public delegate void ThreadStartDelegate(System.Windows.Forms.RichTextBox ric, string msg);        public static void ShowMsg(System.Windows.Forms.RichTextBox ric, string msg)        {            if (!string.IsNullOrEmpty(msg))            {                if (ric.InvokeRequired)//获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中。                {                    ThreadStartDelegate d = new ThreadStartDelegate(ShowMsg);                    ric.BeginInvoke(d, ric, msg);//在创建控件的基础句柄所在线程上,用指定的参数异步执行指定委托。                }                else                {                    ric.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + msg+Environment.NewLine);                    ric.ScrollToCaret();                }            }        }        public static void ShowInfo(System.Windows.Forms.RichTextBox ric, string msg)        {            if (!string.IsNullOrEmpty(msg))            {                if (ric.InvokeRequired)                {                    ThreadStartDelegate d = new ThreadStartDelegate(ShowInfo);                    ric.BeginInvoke(d, ric, msg);                }                else                {                    ric.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 报告单编号为【" + msg +"】的记录上传成功!"+ Environment.NewLine);                    ric.ScrollToCaret();                }            }        }        public static void ShowInfo(System.Windows.Forms.RichTextBox ric, string msg,string ex)        {            if (!string.IsNullOrEmpty(msg))            {                if (ric.InvokeRequired)                {                    ThreadStartDelegate d = new ThreadStartDelegate(ShowInfo);                    ric.BeginInvoke(d, ric, msg);                }                else                {                    ric.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 报告单编号为【" + msg + "】的记录上传失败!"+ex + Environment.NewLine);                    ric.ScrollToCaret();                }            }        }        public static bool SysTest(System.Windows.Forms.RichTextBox ric)        {            SqlConnection localconn = ConnManager.GetLoaclConn();            OleDbConnection remoteconn = ConnManager.GetRemoteConn();            if (ConnManager.OpenSqlConn(localconn))            {                ConnManager.CloseSqlConn(localconn);                ShowMsg(ric, "本地数据库连接成功!");                if (ConnManager.OpenOleDbConn(remoteconn))                {                    ConnManager.CloseOleDbConn(remoteconn);                    ShowMsg(ric, "远程数据库连接成功!");                    return true;                }                else                {                    ConnManager.CloseOleDbConn(remoteconn);                    ShowMsg(ric, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  远程数据库连接失败!");                    return false;                }            }            else            {                ShowMsg(ric, "本地数据库连接成功!");                return false;            }        }    }

0 0
原创粉丝点击