Friday, October 25, 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>
        /// <param name="control"></param>
        /// <returns></returns>
        public static String Text( [CanBeNull] this Control control ) {
            if ( null == control ) {
                return String.Empty;
            }
            return control.InvokeRequired ? control.Invoke( new Func<string>( () => control.Text ) ) as String ?? String.Empty : control.Text;
        }

No comments: