/// <summary> /// Enables accessing the form from another thread. /// The parameters match those of AccessForm() /// </summary> /// /// <param name="action"> a string that names the action to perform on the form </param> /// <param name="formText"> text that the form displays </param> /// <param name="textColor"> a system color for displaying text </param> private void AccessFormMarshal(string action, string textToDisplay, Color textColor) { AccessFormMarshalDelegate AccessFormMarshalDelegate1; AccessFormMarshalDelegate1 = new AccessFormMarshalDelegate(AccessForm); object[] args = { action, textToDisplay, textColor }; // Call AccessForm, passing the parameters in args. base.Invoke(AccessFormMarshalDelegate1, args); }
/// <summary> /// Enables accessing the form from another thread. The parameters match those of AccessForm. /// </summary> /// <remarks> /// Based on methods used by Jan Axelson in Serial Port Complete 2nd edition, except I use /// BeginInvoke rather than Invoke, to avoid the deadlock issue. /// </remarks> /// <param name="userInterfaceAction">Enum that indicates which action to perform</param> /// <param name="comPortData">Com Port data being returned if applicable</param> private void AccessFormMarshal(ComPort.ComPortEvent userInterfaceAction, string comPortData) { AccessFormMarshalDelegate accessFormMarshalDelegate = new AccessFormMarshalDelegate(AccessForm); object[] args = { userInterfaceAction, comPortData }; //Call AccessForm, passing the parameters in args. I use BeginInvoke, after reading several sources indicating //it should be used to avoid a deadlock when the port is closed. Initially I saw it in Alex F.'s comments here: //http://forums.codeguru.com/showthread.php?516248-RESOLVED-Serial-port-hangs-on-form-close, more explanation here: //http://blogs.msdn.com/b/bclteam/archive/2006/10/10/top-5-serialport-tips-_5b00_kim-hamilton_5d00_.aspx. I'm not //sure if there are any downsides yet, but it fixed the deadlock for me, and everything still seems to work okay. base.BeginInvoke(accessFormMarshalDelegate, args); }
/// <summary> /// Enables accessing the form from another thread. /// The parameters match those of AccessForm() /// </summary> /// /// <param name="action"> a string that names the action to perform on the form </param> /// <param name="formText"> text that the form displays </param> /// <param name="textColor"> a system color for displaying text </param> private void AccessFormMarshal( string action, string textToDisplay, Color textColor ) { AccessFormMarshalDelegate AccessFormMarshalDelegate1; AccessFormMarshalDelegate1 = new AccessFormMarshalDelegate( AccessForm ); object[] args = { action, textToDisplay, textColor }; // Call AccessForm, passing the parameters in args. base.Invoke(AccessFormMarshalDelegate1, args); }