Saturday, September 21, 2013

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


        /// <summary>
        ///     Safely set the <see cref="CheckBox.Checked"/> of the CheckBox across threads.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        public static void Checked( 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();
            }
        }

No comments: