C#关于多线程之线程中打开并调用窗体内的方法实例

第一步:如何在线程中打开窗体

            SendEmailProgress  progress=new SendEmailProgress();
            //添加窗体关闭事件
                progress.Closing += (s, ev) =>
                {
                    if (progress.IsEnabledCloseForm)
                    {
                    }
                    else
                    {
                        ev.Cancel = true;//关闭窗体
                    }

                };
                Control.CheckForIllegalCrossThreadCalls = false; //在这个类中我们不检查跨线程的调用是否合法
                Thread thread = new Thread(new ParameterizedThreadStart(ShowCommonProgressFormDialog));
                thread.Start();
                if (isFirst)
                {
                    Thread.Sleep(5000);
                    isFirst = false;
                }
               progress.WriteMessage("","","");//

        private static void ShowCommonProgressFormDialog(object message)
        {
            try
            {
                int num = (int)progress.ShowDialog();
            }
            catch (Exception)
            {
            }
        }

第二步:调用窗体内的方法。

private delegate void WriteMessageHandler(string message, EnumColorInts eci, bool isNeedSetSize);
        /// <summary>
        /// 显示提示消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="eci"></param>
        private void WriteMessage(string message, EnumColorInts eci,bool isNeedSetSize)
        {
            Font font = new Font(FontFamily.GenericMonospace, 14, FontStyle.Regular);  
            //如果控件的 System.Windows.Forms.Control.Handle 是在与调用线程不同的线程上创建的(说明您必须通过 Invoke
            //方法对控件进行调用),则为 true;否则为 false。
            //(备注:获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中。)

            if (InvokeRequired)
            {
                WriteMessageHandler handler = WriteMessage;
                Invoke(handler, new object[] { message, eci, isNeedSetSize });
            }
            else
            {
                int index = RtMessageBox.Text.Length;
                RtMessageBox.AppendText(message);
                RtMessageBox.Select(index, message.Length);
                else
                {
                    switch (eci)
                    {
                        case EnumColorInts.Red:
                            RtMessageBox.SelectionColor = Color.Red;
                            break;
                        case EnumColorInts.Green:
                            RtMessageBox.SelectionColor = Color.Green;
                            break;
                        case EnumColorInts.Black:
                        default:
                            RtMessageBox.SelectionColor = Color.Black;
                            break;
                    }
                }
                RtMessageBox.AppendText("\r\n");
                RtMessageBox.SelectionStart = RtMessageBox.Text.Length;
                RtMessageBox.ScrollToCaret();
            }
        }

以上仅作为个人学习积累。。。。。。

posted @ 2018-02-01 14:57  蚁农  阅读(2991)  评论(0编辑  收藏  举报