Saturday, September 21, 2013

C#: Safely get the Text of a Control across threads.


        /// <summary>
        ///     Safely get the <see cref="Control.Text"/> of a <see cref="Control"/> across threads.
        /// </summary>
        /// <remarks>
        ///     http://kristofverbiest.blogspot.com/2007/02/don-confuse-controlbegininvoke-with.html
        /// </remarks>
        /// <param name="control"></param>
        /// <returns></returns>
        public static String TextGet( this Control control ) {
            if ( null == control ) {
                return String.Empty;
            }
            return control.InvokeRequired ? control.Invoke( new Func<string>( () => control.Text ) ) as String : control.Text;
        }

No comments: