-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControlExtensions.cs
42 lines (40 loc) · 1.54 KB
/
ControlExtensions.cs
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
using System;
using System.Windows.Forms;
namespace Fishing
{
static class ControlExtensions
{
/// <summary>
/// Helper function to make UI operations threadsafe. Call with on a Control, eg.
/// this.UIThread(function); This method is asynchronous, so it is best for quick
/// calls, eg. to update UI elements when the caller isn't waiting for a return.
/// </summary>
/// <param name="control">Control to get thread from for invocation</param>
/// <param name="code">Function to invoke</param>
static public void UIThread(this Control control, Action code)
{
if (control.InvokeRequired)
{
control.BeginInvoke(code);
return;
}
code.Invoke();
}
/// <summary>
/// Helper function to make UI operations threadsafe. Call with on a Control, eg.
/// this.UIThreadInvoke(function); This method blocks until the passed Action has
/// completed, so it is best used when the passed function sets an output variable.
/// </summary>
/// <param name="control">Control to get thread from for invocation</param>
/// <param name="code">Function to invoke</param>
static public void UIThreadInvoke(this Control control, Action code)
{
if (control.InvokeRequired)
{
control.Invoke(code);
return;
}
code.Invoke();
}
}
}