Skip to content

Commit 2ca19aa

Browse files
committed
v1.1
第一个正式可用版,本工具用于下载GitHub仓库中的指定目录,支持两种下载方式、加速服务和代理选项。 支持Unity Package Manager风格的Git URL格式解析。 支持自动获取分支、标签和Commits列表。
1 parent 1377d43 commit 2ca19aa

15 files changed

Lines changed: 5112 additions & 0 deletions

ControlExtensions.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
namespace RS.GitSubDirectoryDownloader
2+
{
3+
/// <summary>
4+
/// Control安全访问扩展方法
5+
/// 解决跨线程访问WinForm控件问题
6+
/// </summary>
7+
public static class ControlExtensions
8+
{
9+
/// <summary>
10+
/// 线程安全地执行操作
11+
/// </summary>
12+
/// <param name="control">控件</param>
13+
/// <param name="action">要执行的操作</param>
14+
public static void SafeInvoke(this Control control, Action action)
15+
{
16+
if (control == null) return;
17+
18+
if (control.InvokeRequired)
19+
{
20+
control.Invoke(action);
21+
}
22+
else
23+
{
24+
action();
25+
}
26+
}
27+
28+
/// <summary>
29+
/// 线程安全地获取控件属性值
30+
/// </summary>
31+
/// <typeparam name="T">返回类型</typeparam>
32+
/// <param name="control">控件</param>
33+
/// <param name="func">获取值的函数</param>
34+
/// <returns>属性值</returns>
35+
public static T SafeGet<T>(this Control control, Func<T> func)
36+
{
37+
if (control == null) return default!;
38+
39+
if (control.InvokeRequired)
40+
{
41+
return (T)control.Invoke(func);
42+
}
43+
return func();
44+
}
45+
46+
/// <summary>
47+
/// 线程安全地设置文本
48+
/// </summary>
49+
public static void SafeSetText(this TextBox textBox, string text)
50+
{
51+
textBox.SafeInvoke(() => textBox.Text = text);
52+
}
53+
54+
/// <summary>
55+
/// 线程安全地获取文本
56+
/// </summary>
57+
public static string SafeGetText(this TextBox textBox)
58+
{
59+
return textBox.SafeGet(() => textBox.Text);
60+
}
61+
62+
/// <summary>
63+
/// 线程安全地设置标签文本
64+
/// </summary>
65+
public static void SafeSetText(this Label label, string text)
66+
{
67+
label.SafeInvoke(() => label.Text = text);
68+
}
69+
70+
/// <summary>
71+
/// 线程安全地设置进度条值
72+
/// </summary>
73+
public static void SafeSetValue(this ProgressBar progressBar, int value)
74+
{
75+
progressBar.SafeInvoke(() => progressBar.Value = value);
76+
}
77+
78+
/// <summary
79+
/// 线程安全地设置进度条最大值
80+
/// </summary>
81+
public static void SafeSetMaximum(this ProgressBar progressBar, int maximum)
82+
{
83+
progressBar.SafeInvoke(() => progressBar.Maximum = maximum);
84+
}
85+
86+
/// <summary>
87+
/// 线程安全地设置按钮文本
88+
/// </summary>
89+
public static void SafeSetText(this Button button, string text)
90+
{
91+
button.SafeInvoke(() => button.Text = text);
92+
}
93+
94+
/// <summary>
95+
/// 线程安全地设置按钮背景色
96+
/// </summary>
97+
public static void SafeSetBackColor(this Button button, Color color)
98+
{
99+
button.SafeInvoke(() => button.BackColor = color);
100+
}
101+
102+
/// <summary>
103+
/// 线程安全地获取ComboBox文本
104+
/// </summary>
105+
public static string SafeGetText(this ComboBox comboBox)
106+
{
107+
return comboBox.SafeGet(() => comboBox.Text);
108+
}
109+
110+
/// <summary>
111+
/// 线程安全地设置ComboBox文本
112+
/// </summary>
113+
public static void SafeSetText(this ComboBox comboBox, string text)
114+
{
115+
comboBox.SafeInvoke(() => comboBox.Text = text);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)