Example #1
0
        public static void DemoAnonymousMethod()
        {
            ConsoleHelper.WriteCallerMethod();
            WriteMessageDelegate method = delegate(string message) { Console.WriteLine(message); };

            method("Text");
        }
Example #2
0
        public static void DemoLamda()
        {
            ConsoleHelper.WriteCallerMethod();
            WriteMessageDelegate method = (message) => { Console.WriteLine(message); };

            method("Text");
        }
Example #3
0
        private void frmMain_Load(Object sender, EventArgs e)
        {
            m_OFD = new OpenFileDialog();
            gridMain.AllowUserToAddRows = false;
            gridMain.AutoSize           = false;
            gridMain.ScrollBars         = ScrollBars.Both;


            txtServerUrl.Text   = "smtp-mail.outlook.com";//"smtp.office365.com";
            txtMailAccount.Text = "*****@*****.**";
            txtPassword.Text    = "";
            txtReceiver.Text    = "*****@*****.**";
            txtPort.Text        = "587";

            txtConsole.Clear();

            WriteMessage = new WriteMessageDelegate(WriteMsg);
            // 定时器
            m_Timer_Backup          = new System.Timers.Timer();
            m_Timer_Backup.Elapsed += M_Timer_Backup_Elapsed;
            m_Timer_Backup.Interval = TimeSpan.FromMinutes(1).TotalMilliseconds;

            // 加载已保存文件
            GetFileList();

            UpdateControlState();
        }
Example #4
0
        public void WriteMessage(string msg, int threadIndex)
        {
            switch (threadIndex)
            {
            case 0:
                if (this.txtThread1.InvokeRequired)
                {
                    WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                    this.txtThread1.Invoke(d, new object[] { msg, threadIndex });
                }
                else
                {
                    this.txtThread1.AppendText(msg + Environment.NewLine);
                }
                break;

            case 1:
                if (this.txtThread2.InvokeRequired)
                {
                    WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                    this.txtThread2.Invoke(d, new object[] { msg, threadIndex });
                }
                else
                {
                    this.txtThread2.AppendText(msg + Environment.NewLine);
                }
                break;

            case 2:
                if (this.txtThread3.InvokeRequired)
                {
                    WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                    this.txtThread3.Invoke(d, new object[] { msg, threadIndex });
                }
                else
                {
                    this.txtThread3.AppendText(msg + Environment.NewLine);
                }
                break;

            case 3:
                if (this.txtThread4.InvokeRequired)
                {
                    WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                    this.txtThread4.Invoke(d, new object[] { msg, threadIndex });
                }
                else
                {
                    this.txtThread4.AppendText(msg + Environment.NewLine);
                }
                break;

            default:
                break;
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            // To make the delegate point to a specific method you must create an instance of the delegate. This is how the delegate resembles a class.

            WriteMessageDelegate del = new WriteMessageDelegate(WriteMessage); // As the argument you pass in the method that you want the delegate to point to.

            // To invoke the method now all you have to do is to invoke the delegate and give it an argument that matches, which is a string in this case.

            del("Hello from Delegate!");
            del("I can call it again!");
            del("And again and again! :)");
        }
 private void WriteMessage(string msg)
 {
     if (f1.textBox2.InvokeRequired)
     {
         WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
         f1.textBox2.Invoke(d, new object[] { msg });
     }
     else
     {
         f1.textBox2.AppendText(msg + Environment.NewLine);
     }
 }
Example #7
0
        public void DemoDelegate()
        {
            ConsoleHelper.WriteCallerMethod();

            WriteMessageDelegate method = WriteToConsole;

            method("Message in console");

            WriteMessageDelegate method2 = WriteToTrace;

            method2("Message in Trace");
        }
Example #8
0
 private void WriteMessage(string msg)
 {
     if (this.rtbServer.InvokeRequired)
     {
         WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
         this.rtbServer.Invoke(d, new object[] { msg });
     }
     else
     {
         this.rtbServer.AppendText(msg + Environment.NewLine);
     }
 }
Example #9
0
 private void AcceptMessage(string msg, NetworkStream clientStream, string clientId)
 {
     if (this.rtbServer.InvokeRequired)
     {
         WriteMessageDelegate d = new WriteMessageDelegate(AcceptMessage);
         this.rtbServer.Invoke(d, new object[] { msg, clientStream, clientId });
     }
     else
     {
         this.rtbServer.AppendText("Client: " + clientId + ", asks for: " + msg + Environment.NewLine);
         HandleMessage(msg, clientStream);
     }
 }
Example #10
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                RiseWatchStatus();
                this.TopMost = chkTopMost.Checked;
                WriteMessage = new WriteMessageDelegate(DoWriteMessage);

                SetControlState();
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始化失败:" + ex.Message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="ignoreFileTypes"></param>
        /// <param name="ignorefolders"></param>
        public NestedContentFinder(
            string path,
            string[] ignoreFileTypes,
            string[] ignorefolders,
            WriteMessageDelegate writeMessageDelegate
            )
        {
            _path = path;
            _ignoreFileTypes = ignoreFileTypes;
            _ignoreFolders = ignorefolders;

            if (writeMessageDelegate == null)
                _writeMessageDelegate = WriteSelf;
            else
                _writeMessageDelegate = writeMessageDelegate;
        }
Example #12
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceDirectory">Directory to back up.</param>
        /// <param name="targetDirectory">Directory to back up to.</param>
        /// <param name="allowedFileExtensions">Optional (can be null or empty). List of file extensions to allow. If any set, only allowed extensions will be backed up. Extensions must not be preceeded by dots</param>
        /// <param name="blockedFileExtensions">Optional (can be null or empty). List of file extensions to ignore. Extensions must not be preceeded by dots</param>
        /// <param name="blockedFolders">Optional (can be null or empty). List of folders to ignore.</param>
        /// <param name="progressBarUpdateDelegate">Delegate to update UI progress bar.</param>
        /// <param name="writeMessageDelegate">Optional. Delegate to write messages to.</param>
        public BackupProcess(
            string sourceDirectory,
            string targetDirectory,
            List<string> allowedFileExtensions,
            List<string> blockedFileExtensions,
            List<string> blockedFolders,
            WriteMessageDelegate statusUpdateDelegate,
            ProgressBarUpdateDelegate progressBarUpdateDelegate,
            WriteMessageDelegate writeMessageDelegate
            )
        {
            _sourceDirectory = sourceDirectory;
            _targetDirectory = targetDirectory;

            if (allowedFileExtensions != null)
            {
                _allowedFileExtensions = allowedFileExtensions;
            }

            if (blockedFileExtensions != null)
            {
                _blockedFileExtensions = blockedFileExtensions;
            }

            if (blockedFolders != null)
            {
                _blockedFolders = blockedFolders;
            }

            _writeMessageDelegate = writeMessageDelegate == null ? this.WriteSelf : writeMessageDelegate;
            _progressBarUpdateDelegate = progressBarUpdateDelegate == null ? this.ProgressUpdate : progressBarUpdateDelegate;
            _statusUpdateDelegate = statusUpdateDelegate == null ? this.WriteSelf : statusUpdateDelegate;

            this.DeleteOrphans = false;
            this.VersionFiles = false;
        }
Example #13
0
 public HandleDbusSignalDump(WriteMessageDelegate writemessage)
 {
     this.writemessage = writemessage;
 }
 /// <summary>
 /// Message writer.
 /// </summary>
 private void WriteMessage(string msg)
 {
     if (this.rtb_Server.InvokeRequired)
     {
         WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
         this.rtb_Server.Invoke(d, new object[] { msg });
     }
     else
     {
         this.rtb_Server.AppendText(msg + System.Environment.NewLine);
     }
 }
 public HandleDbusSignalDump(WriteMessageDelegate writemessage)
 {
     this.writemessage = writemessage;
 }
        public void writeMessageToTextBox(string message, bool linefeed)
        {
            // InvokeRequired required compares the thread ID of the calling thread
             // to the thread ID of the creating thread. If these threads are
             // different, it returns true.
             if (tb_message.InvokeRequired)
             {
            WriteMessageDelegate d = new WriteMessageDelegate(writeMessageToTextBox);
            Invoke(d, new object[] { message, linefeed });
             }
             else
             {
            // determine if the text box should be cleared
            if (message == "CLEAR")
               tb_message.Text = "";
            else
               tb_message.Text += message;

            // check for adding a carriage return
            if (linefeed)
               tb_message.Text += Environment.NewLine;

            // move the horizontal bar to the end of text
            tb_message.SelectionStart = tb_message.TextLength;
            tb_message.ScrollToCaret();

            // ensure the text box is updated
            this.Update();
             }
        }
Example #17
0
        //private void WriteMessage(string msg)
        //{
        //    if (this.commtxt.InvokeRequired)
        //    {
        //        WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
        //        this.commtxt.Invoke(d, new object[] { msg });
        //    }
        //    else
        //    {
        //        this.commtxt.AppendText(msg + Environment.NewLine);
        //    }
        //}
        private void WriteMessage(string msg, string ctrl_name)
        {
            try
            {
                switch (ctrl_name)
                {
                case "switch_status":
                    if (this.switch_status.InvokeRequired)
                    {
                        WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                        this.switch_status.Invoke(d, new object[] { msg, ctrl_name });
                    }
                    else
                    {
                        this.switch_status.Text = msg;
                    }
                    break;

                case "handShake_status":
                    if (this.handShake_status.InvokeRequired)
                    {
                        WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                        this.handShake_status.Invoke(d, new object[] { msg, ctrl_name });
                    }
                    else
                    {
                        this.handShake_status.Text = msg;
                    }
                    break;

                case "host_ip":
                    if (this.host_ip.InvokeRequired)
                    {
                        WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                        this.host_ip.Invoke(d, new object[] { msg, ctrl_name });
                    }
                    else
                    {
                        this.host_ip.Text = msg;
                    }
                    break;

                case "host_ports":
                    if (this.host_ports.InvokeRequired)
                    {
                        WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                        this.host_ports.Invoke(d, new object[] { msg, ctrl_name });
                    }
                    else
                    {
                        this.host_ports.Text = msg;
                    }
                    break;

                case "commtxt":
                    if (this.commtxt.InvokeRequired)
                    {
                        WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                        this.commtxt.Invoke(d, new object[] { msg, ctrl_name });
                    }
                    else
                    {
                        this.commtxt.AppendText(msg + Environment.NewLine);
                    }
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
            };
        }
Example #18
0
        /// <summary>
        /// Routine Summary:
        ///
        ///    private void initializeTabCommunications();
        ///
        /// Description:
        ///
        ///    Routine to initialize variables for the LCD tab.
        ///
        /// Return Value:
        ///
        ///    None.
        ///
        /// Modification History:
        ///
        ///     2/07/13 JWY - First version of routine.
        /// </summary>
        private void initializeTabCommunications()
        {
            // set the delegate member variable
             m_writeMessageToCommunicationsTextBox_Delegate = writeMessageToCommunicationsTextBox;

             // do not enable the send button until communications have been established
             btn_communicationsSend.Enabled = false;
             tb_communicationsCommand.Enabled = false;
        }
Example #19
0
        private void WriteMessage(string keyword)
        {
            if (this.rtbServer.InvokeRequired)
            {
                URLS_FOUND = FindKeywordOccurances(keyword);

                WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                this.rtbServer.Invoke(d, new object[] { keyword });

            }
            else
            {
                this.rtbServer.AppendText("Client is searching for: " + keyword + Environment.NewLine);
            }
        }
Example #20
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jobList"></param>
        /// <param name="writeMessageDelegate">Leave null if not required.</param>
        public BackupSteerer(
            XmlDocument jobList,
            WriteMessageDelegate statuUpdateDelegate,
            ProgressBarUpdateDelegate progressBarUpdateDelegate,
            WriteMessageDelegate writeMessageDelegate,
            UiInvoke uiResetDelegate
            )
        {
            _writeMessageDelegate = writeMessageDelegate;
            _uiResetDelegate = uiResetDelegate;

            XmlNodeList jobs = jobList.DocumentElement.SelectNodes("process");
            foreach (XmlNode job in jobs)
            {
                List<string> allowedFileExtensions = new List<string>();
                List<string> blockedFileExtensions = new List<string>();
                List<string> blockedfolders = new List<string>();

                // get allowed/blocked types
                XmlNodeList types = job.SelectNodes("allowList/type");
                foreach (XmlNode t in types)
                {
                    string s = t.InnerText.ToLower();
                    while (s.StartsWith("."))
                        s = s.Substring(1, s.Length - 1);
                    allowedFileExtensions.Add(s);
                }
                types = job.SelectNodes("blockList/type");
                foreach (XmlNode t in types)
                {
                    string s = t.InnerText.ToLower();
                    while (s.StartsWith("."))
                        s = s.Substring(1, s.Length - 1);
                    blockedFileExtensions.Add(s);
                }

                // get blocked folders
                XmlNodeList folders = job.SelectNodes("blockedFolders/blockedFolder");
                foreach (XmlNode f in folders)
                {
                    string s = f.InnerText.ToLower();
                    blockedfolders.Add(s);
                }

                BackupProcess p = new BackupProcess(
                    job.Attributes["sourceDirectory"].Value,
                    job.Attributes["targetDirectory"].Value,
                    allowedFileExtensions,
                    blockedFileExtensions,
                    blockedfolders,
                    statuUpdateDelegate,
                    progressBarUpdateDelegate,
                    writeMessageDelegate);

                if (job.Attributes["versionFiles"] != null)
                    p.VersionFiles = Boolean.Parse(job.Attributes["versionFiles"].Value);
                if (job.Attributes["deleteOrphans"] != null)
                    p.DeleteOrphans = Boolean.Parse(job.Attributes["deleteOrphans"].Value);

                _jobs.Add(p);
            }
        }