2025-04-15 12:10:19 +02:00

72 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EstrazioneAnagrafica
{
public static class ThreadHelper
{
delegate void SetTextCallback(Form f, Control ctrl, string text);
delegate void SetStatusCallback (Form f, Control ctrl, bool enabled);
delegate void SetVisibilityCallback (Form f, Control ctrl, bool visible);
/// <summary>
/// Set text property of various controls
/// </summary>
/// <param name="form">The calling form</param>
/// <param name="ctrl"></param>
/// <param name="text"></param>
public static void SetText(Form form, Control ctrl, string text)
{
// 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 (ctrl.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
form.Invoke(d, new object[] { form, ctrl, text });
}
else
{
ctrl.Text = text;
}
}
public static void SetStatus(Form form, Control ctrl, bool enabled)
{
// 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 (ctrl.InvokeRequired)
{
SetStatusCallback d = new SetStatusCallback(SetStatus);
form.Invoke(d, new object[] { form, ctrl, enabled });
}
else
{
ctrl.Enabled = enabled;
}
}
public static void SetVisibility(Form form, Control ctrl, 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 (ctrl.InvokeRequired)
{
SetVisibilityCallback d = new SetVisibilityCallback(SetVisibility);
form.Invoke(d, new object[] { form, ctrl, visible });
}
else
{
ctrl.Visible = visible;
}
}
}
}