private void SetVisibility(Control c, bool visible) { if (c.InvokeRequired) { SetVisibilityCallback cb = new SetVisibilityCallback(SetVisibility); this.Invoke(cb, new object[] { c, visible }); } else { c.Visible = visible; } }
public void SetVisibility(bool check) { //Thread Safe if (this.InvokeRequired) { SetVisibilityCallback d = new SetVisibilityCallback(SetVisibility); this.Invoke(d, new object[] { check }); } else { butConnect.Enabled = check; butDisconnect.Enabled = !check; txtNick.Enabled = check; txtIp.Enabled = check; txtPort.Enabled = check; if (check) { c = null; } } }
//used for setting visibility also out of principal threading private void setVisibility(string controlName, bool value) { Control control = this.Controls.Find(controlName, true).FirstOrDefault(); try { if (control.InvokeRequired) { SetVisibilityCallback d = new SetVisibilityCallback(setVisibility); this.Invoke(d, new object[] { controlName, value }); } else { control.Visible = value; } } catch { //probably you've close the form before this thread finish. } }
/// <summary> /// Sets the visibility for asynchronous calls in a thread safe manner. /// </summary> /// <param name="control">The control.</param> /// <param name="visible">The visibility is set to <paramref name="visible"/>.</param> protected void SetVisibilitySafe(Control control, bool visible) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (control.InvokeRequired) { SetVisibilityCallback d = new SetVisibilityCallback(SetVisibilitySafe); Invoke(d, new object[] {control, visible}); } else { control.Visible = visible; } }