forked from nefarius/ScpToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWPFThreadingExtensions.cs
More file actions
180 lines (165 loc) · 7.34 KB
/
WPFThreadingExtensions.cs
File metadata and controls
180 lines (165 loc) · 7.34 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace ScpTrayApp
{
public static class WpfThreadingExtensions
{
/// <summary>
/// Simple helper extension method to marshall to correct
/// thread if its required
/// </summary>
/// <param name="control">The source control</param>
/// <param name="methodcall">The method to call</param>
/// <param name="priorityForCall">The thread priority</param>
public static void InvokeIfRequired(this DispatcherObject control, Action methodcall,
DispatcherPriority priorityForCall)
{
//see if we need to Invoke call to Dispatcher thread
if (control.Dispatcher.Thread != Thread.CurrentThread)
control.Dispatcher.Invoke(priorityForCall, methodcall);
else
methodcall();
}
/// <summary>
/// Gets the Visibility of a UIElement in a thread safe way
/// </summary>
/// <param name="uie">The UIElement</param>
/// <returns>The Visibility of the UIElement</returns>
public static Visibility GetVisibilityThreadSafe(this UIElement uie)
{
var vis = Visibility.Hidden;
InvokeIfRequired(uie, () => { vis = uie.Visibility; }, DispatcherPriority.Background);
return vis;
}
/// <summary>
/// Sets the Visibility of a UIElement in a thread safe way
/// </summary>
/// <param name="uie">The UIElement</param>
/// <param name="vis">The required Visibility</param>
public static void SetVisibilityThreadSafe(this UIElement uie, Visibility vis)
{
InvokeIfRequired(uie, () => { uie.Visibility = vis; }, DispatcherPriority.Background);
}
/// <summary>
/// Sets the IsEnabled value in a thread safe way
/// </summary>
/// <param name="uie">Thr UIElement</param>
/// <param name="enabled">The value for the IsEnabled property</param>
public static void SetIsEnabledThreadSafe(this UIElement uie, bool enabled)
{
InvokeIfRequired(uie, () => { uie.IsEnabled = enabled; }, DispatcherPriority.Background);
}
/// <summary>
/// Sets the text of a TextBlock in a thread safe way
/// </summary>
/// <param name="tb">The TextBlock</param>
/// <param name="s">The string for the Text property</param>
public static void SetTextThreadSafe(this TextBlock tb, string s)
{
InvokeIfRequired(tb, () => { tb.Text = s; }, DispatcherPriority.Background);
}
/// <summary>
/// Gets the Text of a TextBox in a thread safe way
/// </summary>
/// <param name="tb">The TextBox</param>
/// <returns>A string containing the Text of the TextBox</returns>
public static string SetTextThreadSafe(this TextBox tb)
{
var s = "";
InvokeIfRequired(tb, () => { s = tb.Text; }, DispatcherPriority.Background);
return s;
}
/// <summary>
/// Sets the text of a TextBox in a thread safe way
/// </summary>
/// <param name="tb">The TextBox</param>
/// <param name="s">The string for the Text property</param>
public static void SetTextThreadSafe(this TextBox tb, string s)
{
InvokeIfRequired(tb, () => { tb.Text = s; }, DispatcherPriority.Background);
}
/// <summary>
/// Gets the Content of a ContentControl in a thread safe way
/// </summary>
/// <param name="cc">The ContentControl</param>
/// <returns>A string containing the Content of the ContentControl</returns>
public static string GetContentThreadSafe(this ContentControl cc)
{
var s = "";
InvokeIfRequired(cc, () => { s = cc.Content.ToString(); }, DispatcherPriority.Background);
return s;
}
/// <summary>
/// Sets the Content of a ContentControl in a thread safe way
/// </summary>
/// <param name="cc">The ContentControl</param>
/// <param name="s">The string to use as the Content of the ContentControl</param>
public static void SetContentThreadSafe(this ContentControl cc, string s)
{
InvokeIfRequired(cc, () => { cc.Content = s; }, DispatcherPriority.Background);
}
/// <summary>
/// Gets the value of the IsChecked property of a CheckBox in a thread safe way
/// </summary>
/// <param name="cb">The CheckBox</param>
/// <returns>A bool containing the value if the IsChecked property</returns>
public static bool GetIsCheckedThreadSafe(this CheckBox cb)
{
bool? val = null;
InvokeIfRequired(cb, () => { val = cb.IsChecked; }, DispatcherPriority.Background);
return (bool) val;
}
/// <summary>
/// Gets the value of the IsChecked property of a RadioButton in a thread safe way
/// </summary>
/// <param name="rb">The RadioButton</param>
/// <returns>A bool containing the value if the IsChecked property</returns>
public static bool GetIsCheckedThreadSafe(this RadioButton rb)
{
bool? val = null;
InvokeIfRequired(rb, () => { val = rb.IsChecked; }, DispatcherPriority.Background);
return (bool) val;
}
/// <summary>
/// Sets the value of the IsChecked property of a RadioButton in a thread safe way
/// </summary>
/// <param name="rb">The RadioButton</param>
/// <param name="val">The value to set the IsChecked property to</param>
public static void SetIsCheckedThreadSafe(this RadioButton rb, bool? val)
{
InvokeIfRequired(rb, () => { rb.IsChecked = val; }, DispatcherPriority.Background);
}
/// <summary>
/// Sets the value of the IsChecked property of a CheckBox in a thread safe way
/// </summary>
/// <param name="cb">The CheckBox</param>
/// <param name="val">The value to set the IsChecked property to</param>
public static void SetIsCheckedThreadSafe(this CheckBox cb, bool? val)
{
InvokeIfRequired(cb, () => { cb.IsChecked = val; }, DispatcherPriority.Background);
}
/// <summary>
/// Gets the Value property of a ProgressBar in a thread safe way
/// </summary>
/// <param name="pb">The ProgressBar</param>
/// <returns>a double containing the Value property of the ProgressBar</returns>
public static double GetValueThreadSafe(this ProgressBar pb)
{
double val = 0;
InvokeIfRequired(pb, () => { val = pb.Value; }, DispatcherPriority.Background);
return val;
}
/// <summary>
/// Sets the Value property of a ProgressBar in a thread safe way
/// </summary>
/// <param name="pb">the ProgressBar</param>
/// <param name="val">The value to Set the Value property to</param>
public static void SetValueThreadSafe(this ProgressBar pb, double val)
{
InvokeIfRequired(pb, () => { pb.Value = val; }, DispatcherPriority.Background);
}
}
}