diff options
author | Martin Baulig <martin.baulig@googlemail.com> | 2012-06-02 23:52:11 +0400 |
---|---|---|
committer | Martin Baulig <martin.baulig@googlemail.com> | 2012-06-02 23:53:22 +0400 |
commit | efdd059bdd54f11c4d276cb80137fc9983966123 (patch) | |
tree | d275b655f6f49c3d20b8577bfac6e01cbb87395c /mcs/class/System.XML/System.Xml | |
parent | e0e29c61406a3cbf2be6d7e7ff694f9aa8a82bbd (diff) |
Add .NET 4.5 async methods to XmlReader.
Diffstat (limited to 'mcs/class/System.XML/System.Xml')
-rw-r--r-- | mcs/class/System.XML/System.Xml/XmlReader.cs | 233 | ||||
-rw-r--r-- | mcs/class/System.XML/System.Xml/XmlReaderSettings.cs | 30 |
2 files changed, 261 insertions, 2 deletions
diff --git a/mcs/class/System.XML/System.Xml/XmlReader.cs b/mcs/class/System.XML/System.Xml/XmlReader.cs index 38f195aaba6..07d734f80b2 100644 --- a/mcs/class/System.XML/System.Xml/XmlReader.cs +++ b/mcs/class/System.XML/System.Xml/XmlReader.cs @@ -41,6 +41,10 @@ using System.Xml.Serialization; // only required for NET_2_0 (SchemaInfo) using Mono.Xml.Schema; // only required for NET_2_0 #endif using Mono.Xml; // only required for NET_2_0 +#if NET_4_5 +using System.Threading; +using System.Threading.Tasks; +#endif namespace System.Xml { @@ -306,10 +310,15 @@ namespace System.Xml static XmlReaderSettings PopulateSettings (XmlReaderSettings src) { + XmlReaderSettings copy; if (src == null) - return new XmlReaderSettings (); + copy = new XmlReaderSettings (); else - return src.Clone (); + copy = src.Clone (); +#if NET_4_5 + copy.SetReadOnly (); +#endif + return copy; } public static XmlReader Create (Stream input, XmlReaderSettings settings, string baseUri) @@ -1361,5 +1370,225 @@ namespace System.Xml } #endif #endregion + +#if NET_4_5 + bool asyncRunning; + + void StartAsync () + { + if (!settings.Async) + throw new InvalidOperationException ("Set XmlReaderSettings.Async to true if you want to use Async Methods."); + lock (this) { + if (asyncRunning) + throw new InvalidOperationException ("An asynchronous operation is already in progress."); + asyncRunning = true; + } + } + + public virtual Task<bool> ReadAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return Read (); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<string> GetValueAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return Value; + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<string> ReadInnerXmlAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return ReadInnerXml (); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<string> ReadOuterXmlAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return ReadOuterXml (); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<string> ReadContentAsStringAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return ReadContentAsString (); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<int> ReadContentAsBase64Async (byte[] buffer, int index, int count) + { + StartAsync (); + return Task.Run (() => { + try { + return ReadContentAsBase64 (buffer, index, count); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<int> ReadContentAsBinHexAsync (byte[] buffer, int index, int count) + { + StartAsync (); + return Task.Run (() => { + try { + return ReadContentAsBinHex (buffer, index, count); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<int> ReadElementContentAsBase64Async (byte[] buffer, int index, int count) + { + StartAsync (); + return Task.Run (() => { + try { + return ReadElementContentAsBase64 (buffer, index, count); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<int> ReadElementContentAsBinHexAsync (byte[] buffer, int index, int count) + { + StartAsync (); + return Task.Run (() => { + try { + return ReadElementContentAsBinHex (buffer, index, count); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<int> ReadValueChunkAsync (char[] buffer, int index, int count) + { + StartAsync (); + return Task.Run (() => { + try { + return ReadValueChunk (buffer, index, count); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<object> ReadContentAsAsync (Type returnType, IXmlNamespaceResolver namespaceResolver) + { + StartAsync (); + return Task.Run (() => { + try { + return ReadContentAs (returnType, namespaceResolver); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<object> ReadContentAsObjectAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return ReadContentAsObject (); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<object> ReadElementContentAsAsync (Type returnType, IXmlNamespaceResolver namespaceResolver) + { + StartAsync (); + return Task.Run (() => { + try { + return ReadElementContentAs (returnType, namespaceResolver); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<object> ReadElementContentAsObjectAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return ReadElementContentAsObject (); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<string> ReadElementContentAsStringAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return ReadElementContentAsString (); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task<XmlNodeType> MoveToContentAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return MoveToContent (); + } finally { + asyncRunning = false; + } + }); + } + + public virtual Task SkipAsync () + { + StartAsync (); + return Task.Run (() => { + try { + return Skip (); + } finally { + asyncRunning = false; + } + }); + } + +#endif } } diff --git a/mcs/class/System.XML/System.Xml/XmlReaderSettings.cs b/mcs/class/System.XML/System.Xml/XmlReaderSettings.cs index eb7c3b74fc8..70029b2f56d 100644 --- a/mcs/class/System.XML/System.Xml/XmlReaderSettings.cs +++ b/mcs/class/System.XML/System.Xml/XmlReaderSettings.cs @@ -64,6 +64,11 @@ namespace System.Xml private long maxCharactersFromEntities; private long maxCharactersInDocument; +#if NET_4_5 + private bool isReadOnly; + private bool isAsync; +#endif + public XmlReaderSettings () { Reset (); @@ -220,5 +225,30 @@ namespace System.Xml internal get { return xmlResolver; } set { xmlResolver = value; } } + +#if NET_4_5 + internal void SetReadOnly () + { + isReadOnly = true; + } + + /* + * FIXME: The .NET 4.5 runtime throws an exception when attempting to + * modify any of the properties after the XmlReader has been constructed. + */ + void EnsureWritability () + { + if (isReadOnly) + throw new InvalidOperationException ("XmlReaderSettings in read-only"); + } + + public bool Async { + get { return isAsync; } + set { + EnsureWritability (); + isAsync = value; + } + } +#endif } } |