Zzz – Rev 1

Subversion Repositories:
Rev:
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Zzz.Utilities
{
    public static class Miscellaneous
    {
        #region Public Methods

        public static bool LaunchOnBootSet(bool enable)
        {
            using (var key = Registry.CurrentUser.OpenSubKey
                       ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
            {
                if (key == null)
                {
                    return false;
                }

                switch (enable)
                {
                    case true:
                        key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location);
                        break;
                    default:
                        key.DeleteValue(Constants.AssemblyName, false);
                        break;
                }

                return true;
            }
        }

        public static bool LaunchOnBootGet()
        {
            using (var key = Registry.CurrentUser.OpenSubKey
                       ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
            {
                return key?.GetValue(Constants.AssemblyName) != null;
            }
        }

        /// <summary>
        ///     Enable double buffering for an arbitrary control.
        /// </summary>
        /// <param name="control">the control to enable double buffering for</param>
        /// <returns>true on success</returns>
        /// <remarks>Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793</remarks>
        public static void SetDoubleBuffered(this Control control)
        {
            // Double buffering can make DGV slow in remote desktop
            if (!SystemInformation.TerminalServerSession)
            {
                var dgvType = control.GetType();
                var pi = dgvType.GetProperty("DoubleBuffered",
                    BindingFlags.Instance | BindingFlags.NonPublic);
                pi.SetValue(control, true, null);
            }
        }

        public static async Task<Icon> CreateIconFromResource(string resource)
        {
            var iconBytes = await LoadResource(resource);
            using (var iconMemoryStream = new MemoryStream(iconBytes))
            {
                var bitmap = (Bitmap)Image.FromStream(iconMemoryStream);
                var bitmapIntPtr = bitmap.GetHicon();
                var icon = Icon.FromHandle(bitmapIntPtr);
                return icon;
            }
        }

        public static async Task<byte[]> LoadResource(string resource)
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var manifestResourceStream = assembly.GetManifestResourceStream(resource))
            {
                if (manifestResourceStream == null)
                {
                    return null;
                }

                var memoryStream = new MemoryStream();

                await manifestResourceStream.CopyToAsync(memoryStream);

                memoryStream.Position = 0L;

                return memoryStream.ToArray();
            }
        }

        public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : Control
        {
            if (control.InvokeRequired)
            {
                control.BeginInvoke((MethodInvoker)delegate { action(control); });
                return;
            }

            action(control);
        }

        /// <summary>
        ///     Attempts to access a file within <see cref="timeout" /> milliseconds by retrying to access the file every
        ///     <see cref="retry" /> milliseconds.
        /// </summary>
        /// <param name="path">the path to the file</param>
        /// <param name="mode">the file mode used to access the file</param>
        /// <param name="access">the file access to use</param>
        /// <param name="share">the file share to use</param>
        /// <param name="cancellationToken">a cancellation token</param>
        /// <param name="retry">the amount of milliseconds to retry between accesses to the file</param>
        /// <param name="timeout">the amount of time in milliseconds to attempt and access the file</param>
        /// <returns>a file stream if the file could be accessed within the allotted time</returns>
        public static async Task<FileStream> GetFileStream(string path, FileMode mode, FileAccess access,
                                                           FileShare share, CancellationToken cancellationToken,
                                                           int retry = 1000, int timeout = 60000)
        {
            var time = Stopwatch.StartNew();

            while (time.ElapsedMilliseconds < timeout && !cancellationToken.IsCancellationRequested)
            {
                try
                {
                    return new FileStream(path, mode, access, share);
                }
                catch (IOException e)
                {
                    // access error
                    if (e.HResult != -2147024864)
                    {
                        throw;
                    }
                }

                await Task.Delay(retry, cancellationToken);
            }

            throw new TimeoutException($"Failed to get a access to {path} within {timeout}ms.");
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.