From eaaaa2f2d4181920f4c8278086645ad7960ed37a Mon Sep 17 00:00:00 2001 From: David Hall Date: Sat, 15 Dec 2018 11:56:24 -0700 Subject: [PATCH] Extended .NET 2, 3, and 4 builds to include Tasks from Theraot library --- Core/InteropServices/SafeByteArray.cs | 129 ++++++++++------------------- System/BITS/BackgroundCopyJobCollection.cs | 12 +-- System/BITS/BackgroundCopyManager.cs | 13 ++- System/Vanara.SystemServices.csproj | 5 +- System/VirtualDisk.cs | 15 +++- WIndows.Forms/Dialogs/ProgressDialog.cs | 6 +- 6 files changed, 74 insertions(+), 106 deletions(-) diff --git a/Core/InteropServices/SafeByteArray.cs b/Core/InteropServices/SafeByteArray.cs index d3ca1194..8db4c7be 100644 --- a/Core/InteropServices/SafeByteArray.cs +++ b/Core/InteropServices/SafeByteArray.cs @@ -11,10 +11,7 @@ namespace Vanara.InteropServices /// /// /// - public class SafeByteArray : SafeMemoryHandle, IList, ICloneable, IList -#if !(NET20 || NET35) - , IStructuralComparable, IStructuralEquatable -#endif + public class SafeByteArray : SafeMemoryHandle, IList, ICloneable, IList, IStructuralComparable, IStructuralEquatable { /// Initializes a new instance of the class from a copy of a managed byte array. /// The array of bytes to copy. @@ -26,12 +23,11 @@ namespace Vanara.InteropServices /// Initializes a new instance of the class and allocates bytes. /// The byte count to allocate. - public SafeByteArray(int byteCount) : base(byteCount) - { - Zero(); - } + public SafeByteArray(int byteCount) : base(byteCount) => Zero(); - /// Initializes a new instance of the class by copying the bytes from another unmanaged array. + /// + /// Initializes a new instance of the class by copying the bytes from another unmanaged array. + /// /// Another unmanaged array. public SafeByteArray(SafeByteArray src) : base(src?.Count ?? 0) { @@ -53,12 +49,17 @@ namespace Vanara.InteropServices /// Gets a value indicating whether the has a fixed size. bool IList.IsFixedSize => true; - /// Gets a value indicating whether access to the is synchronized (thread safe). + /// + /// Gets a value indicating whether access to the is synchronized (thread safe). + /// bool ICollection.IsSynchronized => true; /// Gets an object that can be used to synchronize access to the . object ICollection.SyncRoot => this; + [ExcludeFromCodeCoverage] + private new int Size { get => base.Size; set => base.Size = value; } + /// Gets or sets the at the specified index. /// The . /// The index. @@ -89,38 +90,28 @@ namespace Vanara.InteropServices set => this[index] = (byte)value; } - [ExcludeFromCodeCoverage] - private new int Size { get => base.Size; set => base.Size = value; } - /// Removes all items from the . /// - public void Clear() - { - Size = 0; - } + public void Clear() => Size = 0; /// Creates a new object that is a copy of the current instance. /// A new object that is a copy of this instance. - public object Clone() - { - return new SafeByteArray(this); - } + public object Clone() => new SafeByteArray(this); /// Determines whether the contains a specific value. /// The object to locate in the . - /// true if is found in the ; otherwise, false. - public bool Contains(byte item) - { - return IndexOf(item) != -1; - } + /// + /// true if is found in the ; otherwise, false. + /// + public bool Contains(byte item) => IndexOf(item) != -1; /// - /// Copies the elements of the to an , starting at a particular - /// index. + /// Copies the elements of the to an , starting + /// at a particular index. /// /// - /// The one-dimensional that is the destination of the elements copied from - /// . The must have zero-based indexing. + /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. /// /// The zero-based index in at which copying begins. /// array @@ -166,28 +157,28 @@ namespace Vanara.InteropServices /// Adds an item to the . /// The object to add to the . - /// The position into which the new element was inserted, or -1 to indicate that the item was not inserted into the collection. + /// + /// The position into which the new element was inserted, or -1 to indicate that the item was not inserted into the collection. + /// int IList.Add(object value) => throw new NotSupportedException(); /// Adds an item to the . /// The object to add to the . /// [ExcludeFromCodeCoverage] - void ICollection.Add(byte item) - { - ((IList)this).Add(item); - } + void ICollection.Add(byte item) => ((IList)this).Add(item); -#if !(NET20 || NET35) /// /// Determines whether the current collection object precedes, occurs in the same position as, or follows another object in the sort order. /// /// The object to compare with the current instance. - /// An object that compares members of the current collection object with the corresponding members of . + /// + /// An object that compares members of the current collection object with the corresponding members of . + /// /// - /// An integer that indicates the relationship of the current collection object to , as shown in the following table.Return - /// valueDescription-1The current instance precedes .0The current instance and are equal.1The current - /// instance follows . + /// An integer that indicates the relationship of the current collection object to , as shown in the + /// following table.Return valueDescription-1The current instance precedes .0The current instance and + /// are equal.1The current instance follows . /// int IStructuralComparable.CompareTo(object other, IComparer comparer) { @@ -212,23 +203,19 @@ namespace Vanara.InteropServices } return 0; } -#endif /// Determines whether the contains a specific value. /// The object to locate in the . /// true if the is found in the ; otherwise, false. - bool IList.Contains(object value) - { - return Contains((byte)value); - } + bool IList.Contains(object value) => Contains((byte)value); /// - /// Copies the elements of the to an , starting at a particular - /// index. + /// Copies the elements of the to an , starting at a + /// particular index. /// /// - /// The one-dimensional that is the destination of the elements copied from . - /// The must have zero-based indexing. + /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. /// /// The zero-based index in at which copying begins. /// array @@ -241,7 +228,6 @@ namespace Vanara.InteropServices array.SetValue(this[i], i + index); } -#if !(NET20 || NET35) /// Determines whether the specified , is equal to this instance. /// The to compare with this instance. /// The comparer. @@ -274,13 +260,11 @@ namespace Vanara.InteropServices } return true; } -#endif /// Returns an enumerator that iterates through a collection. /// An object that can be used to iterate through the collection. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); -#if !(NET20 || NET35) /// Returns a hash code for this instance. /// The comparer. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. @@ -295,7 +279,6 @@ namespace Vanara.InteropServices ret = CombineHashCodes(ret, comparer.GetHashCode(this[i])); return ret; } -#endif /// Determines the index of a specific item in the . /// The object to locate in the . @@ -306,59 +289,39 @@ namespace Vanara.InteropServices /// The zero-based index at which should be inserted. /// The object to insert into the . [ExcludeFromCodeCoverage] - void IList.Insert(int index, object value) - { - ((IList)this).Insert(index, (byte)value); - } + void IList.Insert(int index, object value) => ((IList)this).Insert(index, (byte)value); /// Inserts an item to the at the specified index. /// The zero-based index at which should be inserted. /// The object to insert into the . /// - void IList.Insert(int index, byte item) - { - throw new NotSupportedException(); - } + void IList.Insert(int index, byte item) => throw new NotSupportedException(); /// Removes the first occurrence of a specific object from the . /// The object to remove from the . [ExcludeFromCodeCoverage] - void IList.Remove(object value) - { - ((ICollection)this).Remove((byte)value); - } + void IList.Remove(object value) => ((ICollection)this).Remove((byte)value); /// Removes the first occurrence of a specific object from the . /// The object to remove from the . /// - /// true if was successfully removed from the ; otherwise, false. This - /// method also returns false if is not found in the original . + /// true if was successfully removed from the ; + /// otherwise, false. This method also returns false if is not found in the original . /// /// - bool ICollection.Remove(byte item) - { - throw new NotSupportedException(); - } + bool ICollection.Remove(byte item) => throw new NotSupportedException(); /// Removes the item at the specified index. /// The zero-based index of the item to remove. /// - void IList.RemoveAt(int index) - { - throw new NotSupportedException(); - } + void IList.RemoveAt(int index) => throw new NotSupportedException(); + /// Removes the item at the specified index. /// The zero-based index of the item to remove. [ExcludeFromCodeCoverage] - void IList.RemoveAt(int index) - { - ((IList)this).RemoveAt(index); - } + void IList.RemoveAt(int index) => ((IList)this).RemoveAt(index); - private static int CombineHashCodes(int h1, int h2) - { - return ((h1 << 5) + h1) ^ h2; - } + private static int CombineHashCodes(int h1, int h2) => ((h1 << 5) + h1) ^ h2; private static void CopyMemory(IntPtr src, IntPtr dest, int length) { diff --git a/System/BITS/BackgroundCopyJobCollection.cs b/System/BITS/BackgroundCopyJobCollection.cs index beacb310..70dd389b 100644 --- a/System/BITS/BackgroundCopyJobCollection.cs +++ b/System/BITS/BackgroundCopyJobCollection.cs @@ -102,9 +102,9 @@ namespace Vanara.IO } /// Removes the first occurrence of a specific object from the . The object to - /// remove from the . true if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . item + /// remove from the . true if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . item public bool Remove(BackgroundCopyJob item) { if (item == null) throw new ArgumentNullException(nameof(item)); @@ -116,7 +116,7 @@ namespace Vanara.IO /// A that represents this instance. public override string ToString() => $"Jobs: {Count}"; - /// Adds an item to the . The object to add to the . + /// Adds an item to the . The object to add to the . void ICollection.Add(BackgroundCopyJob item) { } @@ -127,9 +127,9 @@ namespace Vanara.IO /// bool ICollection.Contains(BackgroundCopyJob item) => Contains(item.ID); - /// Copies the elements of the to an , starting at a particular index. + /// Copies the elements of the to an , starting at a particular index. /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. The zero-based index in . The must have zero-based indexing. The zero-based index in at which copying begins. void ICollection.CopyTo(BackgroundCopyJob[] array, int arrayIndex) { diff --git a/System/BITS/BackgroundCopyManager.cs b/System/BITS/BackgroundCopyManager.cs index f9751ff3..50fd5144 100644 --- a/System/BITS/BackgroundCopyManager.cs +++ b/System/BITS/BackgroundCopyManager.cs @@ -1,10 +1,8 @@ using System; using System.Runtime.InteropServices; using System.Security.Principal; -#if !(NET20 || NET35 || NET40) using System.Threading; using System.Threading.Tasks; -#endif using Vanara.PInvoke; using static Vanara.PInvoke.BITS; @@ -63,8 +61,6 @@ namespace Vanara.IO CopyTemplate(sourceFileName, destFileName, () => false, System.Threading.Thread.Sleep, null, f => f.Add(sourceFileName, destFileName)); } -#if !(NET20 || NET35 || NET40) - /// Copies an existing file to a new file using BITS. Overwriting a file of the same name is not allowed. /// The file to copy. /// The name of the destination file. @@ -73,13 +69,16 @@ namespace Vanara.IO /// A task that represents the asynchronous copy operation. public static async Task CopyAsync(string sourceFileName, string destFileName, CancellationToken cancellationToken, IProgress> progress) { - await Task.Run(() => CopyTemplate(sourceFileName, destFileName, () => cancellationToken.IsCancellationRequested, +#if NET40 + await TaskEx.Run(() => CopyTemplate(sourceFileName, destFileName, () => cancellationToken.IsCancellationRequested, Thread.Sleep, (s, p) => progress?.Report(new Tuple(s,p)), f => f.Add(sourceFileName, destFileName)), cancellationToken); +#else + await Task.Run(() => CopyTemplate(sourceFileName, destFileName, () => cancellationToken.IsCancellationRequested, + Thread.Sleep, (s, p) => progress?.Report(new Tuple(s, p)), f => f.Add(sourceFileName, destFileName)), cancellationToken); +#endif cancellationToken.ThrowIfCancellationRequested(); } -#endif - private static IBackgroundCopyManager IMgr { get; set; } internal static IBackgroundCopyJob CreateJob(string displayName, BG_JOB_TYPE jobType = BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD) diff --git a/System/Vanara.SystemServices.csproj b/System/Vanara.SystemServices.csproj index 3cd0093c..fa800114 100644 --- a/System/Vanara.SystemServices.csproj +++ b/System/Vanara.SystemServices.csproj @@ -60,8 +60,9 @@ BackgroundCopyACLFlags, BackgroundCopyCost, BackgroundCopyErrorContext, Backgrou - - + + + diff --git a/System/VirtualDisk.cs b/System/VirtualDisk.cs index 97b172e9..7ee80a32 100644 --- a/System/VirtualDisk.cs +++ b/System/VirtualDisk.cs @@ -7,10 +7,8 @@ using System.Linq; using System.Runtime.InteropServices; using System.Security.AccessControl; using System.Text; -#if !(NET20 || NET35 || NET40) using System.Threading; using System.Threading.Tasks; -#endif using Vanara.Extensions; using Vanara.InteropServices; using Vanara.PInvoke; @@ -530,7 +528,6 @@ namespace Vanara.IO ResizeVirtualDisk(Handle, flags, param, IntPtr.Zero).ThrowIfFailed(); } -#if !(NET20 || NET35 || NET40) /// Creates a virtual hard disk (VHD) image file, either using default parameters or using an existing VHD or physical disk. /// A valid file path that represents the path to the new virtual disk image file. /// @@ -569,7 +566,11 @@ namespace Vanara.IO return err; } ); +#if (NET20 || NET35) + if (!b) throw new Theraot.Core.NewOperationCanceledException(cancellationToken); +#else if (!b) throw new OperationCanceledException(cancellationToken); +#endif return new VirtualDisk(new SafeVIRTUAL_DISK_HANDLE(h), (OPEN_VIRTUAL_DISK_VERSION)param.Version); } @@ -648,10 +649,17 @@ namespace Vanara.IO break; } if (prog.CurrentValue == prog.CompletionValue) return true; +#if NET40 + if (cancellationToken == null) + await TaskEx.Delay(250); + else + await TaskEx.Delay(250, cancellationToken); +#else if (cancellationToken == null) await Task.Delay(250); else await Task.Delay(250, cancellationToken); +#endif } } @@ -730,7 +738,6 @@ namespace Vanara.IO void ReportProgress(int percent) { progress.Report(new Tuple(percent, $"Compacting VHD volume \"{loc}\"")); } }*/ -#endif private static SafeSecurityDescriptor FileSecToSd(FileSecurity sec) { diff --git a/WIndows.Forms/Dialogs/ProgressDialog.cs b/WIndows.Forms/Dialogs/ProgressDialog.cs index 5633d0b7..fc8ce297 100644 --- a/WIndows.Forms/Dialogs/ProgressDialog.cs +++ b/WIndows.Forms/Dialogs/ProgressDialog.cs @@ -1,5 +1,4 @@ -#if !(NET20 || NET35 || NET40) -using System; +using System; using System.ComponentModel; using System.Drawing; using System.Threading; @@ -450,5 +449,4 @@ namespace Vanara.Windows.Forms [DefaultValue("")] public string StatusText { get; set; } } -} -#endif \ No newline at end of file +} \ No newline at end of file