1. 程式人生 > >C#關於多線程之線程中打開並調用窗體內的方法實例

C#關於多線程之線程中打開並調用窗體內的方法實例

dstar read 調用 regular app default bject object mail

第一步:如何在線程中打開窗體

            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();
            }
        }

以上僅作為個人學習積累。。。。。。

C#關於多線程之線程中打開並調用窗體內的方法實例