Skip to content

Commit

Permalink
Cross-threading access for check list boxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
phw198 committed Oct 22, 2023
1 parent 21484ac commit 4df25f2
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/OutlookGoogleCalendarSync/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,9 @@ private void miColourRefresh_Click(object sender, EventArgs e) {
}
/// <summary>Shim function to work around x-thread call of BuildPicker()</summary>
public void miColourBuildPicker_Click(object sender, EventArgs e) {
GoogleOgcs.Calendar.Instance.ColourPalette.BuildPicker(clbColours);
CheckedListBox clb = GetControlThreadSafe(clbColours) as CheckedListBox;
GoogleOgcs.Calendar.Instance.ColourPalette.BuildPicker(clb);
SetControlPropertyThreadSafe(clbColours, "Items", clb.Items);
}
private void miColourSelectNone_Click(object sender, EventArgs e) {
for (int i = 0; i < clbColours.Items.Count; i++) {
Expand Down Expand Up @@ -2440,20 +2442,20 @@ private void cbAlphaReleases_CheckedChanged(object sender, EventArgs e) {
#endregion

#region Thread safe access to form components
//private delegate Control getControlThreadSafeDelegate(Control control);
private delegate Control getControlThreadSafeDelegate(Control control);

private delegate void setControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
private delegate object getControlPropertyThreadSafeDelegate(Control control, string propertyName);
private delegate void setControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);

private delegate void callControlMethodThreadSafeDelegate(Control control, string methodName, object methodArgValue);

//private static Control getControlThreadSafe(Control control) {
// if (control.InvokeRequired) {
// return (Control)control.Invoke(new getControlThreadSafeDelegate(getControlThreadSafe), new object[] { control });
// } else {
// return control;
// }
//}
public static Control GetControlThreadSafe(Control control) {
if (control.InvokeRequired) {
return (Control)control.Invoke(new getControlThreadSafeDelegate(GetControlThreadSafe), new object[] { control });
} else {
return control.GetType().InvokeMember("discarded", System.Reflection.BindingFlags.CreateInstance, null, control, null) as Control;
}
}

public object GetControlPropertyThreadSafe(Control control, string propertyName) {
if (control.InvokeRequired) {
Expand All @@ -2466,8 +2468,12 @@ public void SetControlPropertyThreadSafe(Control control, string propertyName, o
if (control.InvokeRequired) {
control.Invoke(new setControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
} else {
if (control is CheckedListBox && propertyValue is CheckedListBox.ObjectCollection) {
(control as CheckedListBox).Items.AddRange(propertyValue as CheckedListBox.ObjectCollection);
return;
}
var theObject = control.GetType().InvokeMember(propertyName, System.Reflection.BindingFlags.SetProperty, null, control, new object[] { propertyValue });
if (control.GetType().Name == "TextBox") {
if (control is TextBox) {
TextBox tb = control as TextBox;
tb.SelectionStart = tb.Text.Length;
tb.ScrollToCaret();
Expand Down

0 comments on commit 4df25f2

Please sign in to comment.