Horizon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Diagnostics; |
||
3 | using System.Drawing; |
||
4 | using System.IO; |
||
5 | using System.Reflection; |
||
6 | using System.Threading; |
||
7 | using System.Threading.Tasks; |
||
8 | using System.Windows.Forms; |
||
9 | using Microsoft.Win32; |
||
10 | |||
11 | namespace Horizon.Utilities |
||
12 | { |
||
13 | public static class Miscellaneous |
||
14 | { |
||
15 | #region Public Methods |
||
16 | |||
17 | public static bool LaunchOnBootSet(bool enable) |
||
18 | { |
||
19 | using (var key = Registry.CurrentUser.OpenSubKey |
||
20 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
||
21 | { |
||
22 | if (key == null) |
||
23 | { |
||
24 | return false; |
||
25 | } |
||
26 | |||
27 | switch (enable) |
||
28 | { |
||
29 | case true: |
||
30 | key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location); |
||
31 | break; |
||
32 | default: |
||
33 | key.DeleteValue(Constants.AssemblyName, false); |
||
34 | break; |
||
35 | } |
||
36 | |||
37 | return true; |
||
38 | } |
||
39 | } |
||
40 | |||
41 | public static bool LaunchOnBootGet() |
||
42 | { |
||
43 | using (var key = Registry.CurrentUser.OpenSubKey |
||
44 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
||
45 | { |
||
46 | return key?.GetValue(Constants.AssemblyName) != null; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /// <summary> |
||
51 | /// Enable double buffering for an arbitrary control. |
||
52 | /// </summary> |
||
53 | /// <param name="control">the control to enable double buffering for</param> |
||
54 | /// <returns>true on success</returns> |
||
55 | /// <remarks>Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793</remarks> |
||
56 | public static void SetDoubleBuffered(this Control control) |
||
57 | { |
||
58 | // Double buffering can make DGV slow in remote desktop |
||
59 | if (!SystemInformation.TerminalServerSession) |
||
60 | { |
||
61 | var dgvType = control.GetType(); |
||
62 | var pi = dgvType.GetProperty("DoubleBuffered", |
||
63 | BindingFlags.Instance | BindingFlags.NonPublic); |
||
64 | pi.SetValue(control, true, null); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | public static async Task<Icon> CreateIconFromResource(string resource) |
||
69 | { |
||
70 | var iconBytes = await LoadResource(resource); |
||
71 | using (var iconMemoryStream = new MemoryStream(iconBytes)) |
||
72 | { |
||
73 | var bitmap = (Bitmap)Image.FromStream(iconMemoryStream); |
||
74 | var bitmapIntPtr = bitmap.GetHicon(); |
||
75 | var icon = Icon.FromHandle(bitmapIntPtr); |
||
76 | return icon; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | public static async Task<byte[]> LoadResource(string resource) |
||
81 | { |
||
82 | var assembly = Assembly.GetExecutingAssembly(); |
||
83 | |||
84 | using (var manifestResourceStream = assembly.GetManifestResourceStream(resource)) |
||
85 | { |
||
86 | if (manifestResourceStream == null) |
||
87 | { |
||
88 | return null; |
||
89 | } |
||
90 | |||
91 | var memoryStream = new MemoryStream(); |
||
92 | |||
93 | await manifestResourceStream.CopyToAsync(memoryStream); |
||
94 | |||
95 | memoryStream.Position = 0L; |
||
96 | |||
97 | return memoryStream.ToArray(); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : Control |
||
102 | { |
||
103 | if (control.InvokeRequired) |
||
104 | { |
||
105 | control.BeginInvoke((MethodInvoker)delegate { action(control); }); |
||
106 | return; |
||
107 | } |
||
108 | |||
109 | action(control); |
||
110 | } |
||
111 | |||
112 | /// <summary> |
||
113 | /// Attempts to access a file within <see cref="timeout" /> milliseconds by retrying to access the file every |
||
114 | /// <see cref="retry" /> milliseconds. |
||
115 | /// </summary> |
||
116 | /// <param name="path">the path to the file</param> |
||
117 | /// <param name="mode">the file mode used to access the file</param> |
||
118 | /// <param name="access">the file access to use</param> |
||
119 | /// <param name="share">the file share to use</param> |
||
120 | /// <param name="cancellationToken">a cancellation token</param> |
||
121 | /// <param name="retry">the amount of milliseconds to retry between accesses to the file</param> |
||
122 | /// <param name="timeout">the amount of time in milliseconds to attempt and access the file</param> |
||
123 | /// <returns>a file stream if the file could be accessed within the allotted time</returns> |
||
124 | public static async Task<FileStream> GetFileStream(string path, FileMode mode, FileAccess access, |
||
125 | FileShare share, CancellationToken cancellationToken, |
||
126 | int retry = 1000, int timeout = 60000) |
||
127 | { |
||
128 | var time = Stopwatch.StartNew(); |
||
129 | |||
130 | while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested) |
||
131 | { |
||
132 | try |
||
133 | { |
||
134 | return new FileStream(path, mode, access, share); |
||
135 | } |
||
136 | catch (IOException e) |
||
137 | { |
||
138 | // access error |
||
139 | if (e.HResult != -2147024864) |
||
140 | { |
||
141 | throw; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | await Task.Delay(retry, cancellationToken); |
||
146 | } |
||
147 | |||
148 | throw new TimeoutException($"Failed to get a access to {path} within {timeout}ms."); |
||
149 | } |
||
150 | |||
151 | #endregion |
||
152 | } |
||
153 | } |