-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadHelperClass.cs
More file actions
106 lines (98 loc) · 3.83 KB
/
ThreadHelperClass.cs
File metadata and controls
106 lines (98 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Windows.Forms;
namespace YoutubeTitleForYvonne
{
/// <summary>
/// Helper class based on code by Thunder of StackOverflow.
/// https://stackoverflow.com/questions/10775367/cross-thread-operation-not-valid-control-textbox1-accessed-from-a-thread-othe/15831292#15831292
/// </summary>
public static class ThreadHelperClass
{
delegate void SetTextCallback(Form f, Control ctrl, string text);
/// <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;
}
}
delegate string GetTextCallback(Form f, Control ctrl);
/// <summary>
/// Get text property of various controls
/// </summary>
/// <param name="form">The calling form</param>
/// <param name="ctrl"></param>
public static string GetText(Form form, Control ctrl)
{
// 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)
{
GetTextCallback d = new GetTextCallback(GetText);
return (string)form.Invoke(d, new object[] { form, ctrl });
}
else
{
return ctrl.Text;
}
}
delegate void SetEnabledCallback(Form f, Control ctrl, bool enabled);
/// <summary>
/// Set enabled property of various controls
/// </summary>
/// <param name="form">The calling form</param>
/// <param name="ctrl"></param>
/// <param name="enabled"></param>
public static void SetEnabled(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)
{
SetEnabledCallback d = new SetEnabledCallback(SetEnabled);
form.Invoke(d, new object[] { form, ctrl, enabled });
}
else
{
ctrl.Enabled = enabled;
}
}
delegate void SetVisibleCallback(Form f, Control ctrl, bool visible);
/// <summary>
/// Set enabled property of various controls
/// </summary>
/// <param name="form">The calling form</param>
/// <param name="ctrl"></param>
/// <param name="visible"></param>
public static void SetVisible(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)
{
SetVisibleCallback d = new SetVisibleCallback(SetVisible);
form.Invoke(d, new object[] { form, ctrl, visible });
}
else
{
ctrl.Visible = visible;
}
}
}
}