private void SetText(ButtonBase tb, string text) { if (tb.InvokeRequired)//如果调用控件的线程和创建创建控件的线程不是同一个则为True { while (!tb.IsHandleCreated) { //解决窗体关闭时出现“访问已释放句柄“的异常 if (tb.Disposing || tb.IsDisposed) { return; } } tb.Invoke(new SetTextBoxText(SetText), new object[] { tb, text }); } else { tb.Text = text; if (tb == btnStart) { tb.Text = text; if (IsRunning()) { tb.BackColor = Color.DarkRed; } else { tb.BackColor = Color.ForestGreen; } } } }
public static void SetEnabledInvoke(this ButtonBase b, bool v) { if (b.InvokeRequired) { b.Invoke((MethodInvoker) delegate { b.SetEnabledInvoke(v); }); } else { b.Enabled = v; } }
public static void SetTextInvoke(this ButtonBase b, String c) { if (b.InvokeRequired) { b.Invoke((MethodInvoker) delegate { b.SetTextInvoke(c); }); } else { b.Text = c; } }
public static void ResetButtonDefaultColor(ButtonBase control) { if (control.InvokeRequired) { control.Invoke((MethodInvoker) delegate { ResetButtonDefaultColor(control); }); } else { control.BackColor = default(Color); control.UseVisualStyleBackColor = true; } }