First step: How to open a form in a thread
Sendemailprogress progress=new sendemailprogress (); Add form Close Event progress. Closing + = (s, ev) = = {if (progress. Isenabledcloseform) {} else { Ev. Cancel = true;//close the form}}; Control.checkforillegalcrossthreadcalls = false; In this class we do not check whether a cross-thread call is legal 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){ } }
Step Two: Call methods in the body of the window.
Private delegate void Writemessagehandler (String message, enumcolorints ECI, bool isneedsetsize); <summary>///Show message///</summary>//<param name= "message" ></param> <param name= "ECI" ></param> private void Writemessage (String message, Enumcolorints Eci,bool is needsetsize) {Font font = new Font (fontfamily.genericmonospace, +, fontstyle.regular); True if the control's System.Windows.Forms.Control.Handle is created on a different thread than the calling thread (indicating that you must call the control via the Invoke//method); (Note: Gets a value that indicates whether the caller must call the Invoke method when making a method call to the control, because the call is in a thread other than the thread on which the control is created.) ) 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.b Lack Break }} rtmessagebox.appendtext ("\ r \ n"); Rtmessagebox.selectionstart = RtMessageBox.Text.Length; Rtmessagebox.scrolltocaret (); } }
The above only as a personal learning accumulation ...
C # Example of a method that opens and calls a window body in a multi-thread thread