Showing posts with label checked. Show all posts
Showing posts with label checked. Show all posts

Saturday, January 25, 2014

C#: Safely set the CheckBox.Checked property of a control across threads.


        /// <summary>
        ///     Safely set the <see cref="CheckBox.Checked" /> of the control across threads.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        public static void Checked( [CanBeNull] this CheckBox control, Boolean value ) {
            if ( null == control ) {
                return;
            }
            if ( control.InvokeRequired ) {
                control.BeginInvoke( new Action( () => {
                    control.Checked = value;
                    control.Refresh();
                } ) );
            }
            else {
                control.Checked = value;
                control.Refresh();
            }
        }

Friday, October 25, 2013

C# : Safely get the Checked() of a Control across threads


        public static Boolean Checked( [CanBeNull] this CheckBox control ) {
            if ( null == control ) {
                return false;
            }
            return control.InvokeRequired ? ( Boolean )control.Invoke( new Func<Boolean>( () => control.Checked ) ) : control.Checked;
        }