Winify – Diff between revs 28 and 30

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