public static T UnmarshalXml <T>(string srcFile) where T : class { if (!File.Exists(srcFile)) { throw new FileNotFoundException(srcFile); } Lock( srcFile, f => { try { var cbuffer = new byte[] { }; f.Write(cbuffer, 0, cbuffer.Length); } catch (IOException e) { throw new Exception(string.Format("Could not unmarshall Object from file:{0}", srcFile), e); } }); using (var fs = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.None, ScenarioDocuGeneratorConfiguration.AsyncWritingBufferSize, true)) { var desirializedObject = ScenarioDocuXMLUtil.UnmarshalXml <T>(fs); fs.Flush(); fs.Close(); return(desirializedObject); } }
/// <summary> /// Starts an async task for to marshall an XML in a Thread-Pool /// </summary> public static void MarshalXml <T>(T entity, string destFile) where T : class { Lock( destFile, f => { try { var cbuffer = new byte[] { }; f.Write(cbuffer, 0, cbuffer.Length); } catch (IOException e) { throw new Exception( string.Format( "Could not marshall Object of type:{0} into file:{1}", entity.GetType().Name, destFile), e); } }); do { if (RunningTasks.Count <= ScenarioDocuGeneratorConfiguration.MaxConcurrentTasks) { var task = new Task( () => { using ( var fs = new FileStream( destFile, FileMode.Create, FileAccess.Write, FileShare.None, ScenarioDocuGeneratorConfiguration.MaxConcurrentTasks, true)) { ScenarioDocuXMLUtil.MarshalXml(entity, fs); fs.Flush(); fs.Close(); } }); task.Start(); RunningTasks.Add(task); } RemoveFinishedTasks(); Thread.Sleep(100); }while (RunningTasks.Count > ScenarioDocuGeneratorConfiguration.MaxConcurrentTasks); }