Action代替普通delegate操作实例

 Action代替普通delegate操作实例,其实action就是一个delegate

使用普通delegate代码实例如下:


        #region 消息展示
        private delegate void dlgShowMsg(string str);
        private void logStr(string str)
        {
            if (richTextBox1.InvokeRequired)
            {
                dlgShowMsg dlg = new dlgShowMsg(logStr);
                richTextBox1.Invoke(dlg, str);
            }
            else
            {
                if (richTextBox1.Lines.Length >= 1000)
                    richTextBox1.Clear();
                richTextBox1.AppendText(DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff") + "->" + str + "\r\n");
            }
        }
        #endregion

使用Action代码如下

        #region 消息展示
        //private delegate void dlgShowMsg(string str);
        
        private void logStr(string str)
        {
            if (richTextBox1.InvokeRequired)
            {
                //dlgShowMsg dlg = new dlgShowMsg(logStr);
                Action dlg = new Action(logStr);
                richTextBox1.Invoke(dlg, str);
            }
            else
            {
                if (richTextBox1.Lines.Length >= 1000)
                    richTextBox1.Clear();
                richTextBox1.AppendText(DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff") + "->" + str + "\r\n");
            }
        }
        #endregion

特此记录

anlog

2024年1月2日

你可能感兴趣的:(笔记,c#,Action,delegate)