ImportGraphMLFilesAsync ( String sourceFolderPath, String destinationFolderPath ) { Debug.Assert(!String.IsNullOrEmpty(sourceFolderPath)); Debug.Assert(Directory.Exists(sourceFolderPath)); Debug.Assert(!String.IsNullOrEmpty(destinationFolderPath)); Debug.Assert(Directory.Exists(destinationFolderPath)); AssertValid(); if (this.IsBusy) { throw new InvalidOperationException( "GraphMLFilesImporter.ImportGraphMLFilesAsync: An asynchronous" + " operation is already in progress." ); } // Wrap the arguments in an object that can be passed to // BackgroundWorker.RunWorkerAsync(). ImportGraphMLFilesAsyncArgs oImportGraphMLFilesAsyncArgs = new ImportGraphMLFilesAsyncArgs(); oImportGraphMLFilesAsyncArgs.SourceFolderPath = sourceFolderPath; oImportGraphMLFilesAsyncArgs.DestinationFolderPath = destinationFolderPath; // Create a BackgroundWorker and handle its events. m_oBackgroundWorker = new BackgroundWorker(); m_oBackgroundWorker.WorkerReportsProgress = true; m_oBackgroundWorker.WorkerSupportsCancellation = true; m_oBackgroundWorker.DoWork += new DoWorkEventHandler( BackgroundWorker_DoWork); m_oBackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged); m_oBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( BackgroundWorker_RunWorkerCompleted); m_oBackgroundWorker.RunWorkerAsync(oImportGraphMLFilesAsyncArgs); }
BackgroundWorker_DoWork ( object sender, DoWorkEventArgs e ) { Debug.Assert(sender is BackgroundWorker); AssertValid(); Debug.Assert(e.Argument is ImportGraphMLFilesAsyncArgs); ImportGraphMLFilesAsyncArgs oImportGraphMLFilesAsyncArgs = (ImportGraphMLFilesAsyncArgs)e.Argument; ImportGraphMLFilesInternal(oImportGraphMLFilesAsyncArgs, m_oBackgroundWorker, e); }
ImportGraphMLFilesInternal ( ImportGraphMLFilesAsyncArgs oImportGraphMLFilesAsyncArgs, BackgroundWorker oBackgroundWorker, DoWorkEventArgs oDoWorkEventArgs ) { Debug.Assert(oImportGraphMLFilesAsyncArgs != null); Debug.Assert(oBackgroundWorker != null); Debug.Assert(oDoWorkEventArgs != null); AssertValid(); Int32 iGraphMLFiles = 0; foreach ( String sGraphMLFilePath in Directory.GetFiles( oImportGraphMLFilesAsyncArgs.SourceFolderPath, "*.graphml") ) { if (oBackgroundWorker.CancellationPending) { oDoWorkEventArgs.Cancel = true; return; } String sGraphMLFileName = Path.GetFileName(sGraphMLFilePath); oBackgroundWorker.ReportProgress(0, String.Format( "Importing \"{0}\"." , sGraphMLFileName ) ); XmlDocument oGraphMLDocument = new XmlDocument(); try { oGraphMLDocument.Load(sGraphMLFilePath); } catch (XmlException oXmlException) { OnXmlException(sGraphMLFileName, oXmlException); continue; } String sNodeXLWorkbookPath = Path.Combine( oImportGraphMLFilesAsyncArgs.DestinationFolderPath, sGraphMLFileName ); sNodeXLWorkbookPath = Path.ChangeExtension(sNodeXLWorkbookPath, ".xlsx"); try { GraphMLToNodeXLWorkbookConverter.SaveGraphToNodeXLWorkbook( oGraphMLDocument, sGraphMLFilePath, sNodeXLWorkbookPath, null, false); } catch (XmlException oXmlException) { OnXmlException(sGraphMLFileName, oXmlException); continue; } iGraphMLFiles++; } oBackgroundWorker.ReportProgress(0, String.Format( "Done. GraphML files imported: {0}." , iGraphMLFiles ) ); }
ImportGraphMLFilesAsync ( String sourceFolderPath, String destinationFolderPath ) { Debug.Assert( !String.IsNullOrEmpty(sourceFolderPath) ); Debug.Assert( Directory.Exists(sourceFolderPath) ); Debug.Assert( !String.IsNullOrEmpty(destinationFolderPath) ); Debug.Assert( Directory.Exists(destinationFolderPath) ); AssertValid(); if (this.IsBusy) { throw new InvalidOperationException( "GraphMLFilesImporter.ImportGraphMLFilesAsync: An asynchronous" + " operation is already in progress." ); } // Wrap the arguments in an object that can be passed to // BackgroundWorker.RunWorkerAsync(). ImportGraphMLFilesAsyncArgs oImportGraphMLFilesAsyncArgs = new ImportGraphMLFilesAsyncArgs(); oImportGraphMLFilesAsyncArgs.SourceFolderPath = sourceFolderPath; oImportGraphMLFilesAsyncArgs.DestinationFolderPath = destinationFolderPath; // Create a BackgroundWorker and handle its events. m_oBackgroundWorker = new BackgroundWorker(); m_oBackgroundWorker.WorkerReportsProgress = true; m_oBackgroundWorker.WorkerSupportsCancellation = true; m_oBackgroundWorker.DoWork += new DoWorkEventHandler( BackgroundWorker_DoWork); m_oBackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged); m_oBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( BackgroundWorker_RunWorkerCompleted); m_oBackgroundWorker.RunWorkerAsync(oImportGraphMLFilesAsyncArgs); }
ImportGraphMLFilesInternal ( ImportGraphMLFilesAsyncArgs oImportGraphMLFilesAsyncArgs, BackgroundWorker oBackgroundWorker, DoWorkEventArgs oDoWorkEventArgs ) { Debug.Assert(oImportGraphMLFilesAsyncArgs != null); Debug.Assert(oBackgroundWorker != null); Debug.Assert(oDoWorkEventArgs != null); AssertValid(); Int32 iGraphMLFiles = 0; foreach (String sGraphMLFilePath in Directory.GetFiles( oImportGraphMLFilesAsyncArgs.SourceFolderPath, "*.graphml")) { if (oBackgroundWorker.CancellationPending) { oDoWorkEventArgs.Cancel = true; return; } String sGraphMLFileName = Path.GetFileName(sGraphMLFilePath); oBackgroundWorker.ReportProgress(0, String.Format( "Importing \"{0}\"." , sGraphMLFileName )); XmlDocument oGraphMLDocument = new XmlDocument(); try { oGraphMLDocument.Load(sGraphMLFilePath); } catch (XmlException oXmlException) { OnXmlException(sGraphMLFileName, oXmlException); continue; } String sNodeXLWorkbookPath = Path.Combine( oImportGraphMLFilesAsyncArgs.DestinationFolderPath, sGraphMLFileName ); sNodeXLWorkbookPath = Path.ChangeExtension(sNodeXLWorkbookPath, ".xlsx"); try { GraphMLToNodeXLWorkbookConverter.SaveGraphToNodeXLWorkbook( oGraphMLDocument, sGraphMLFilePath, sNodeXLWorkbookPath, null, false); } catch (XmlException oXmlException) { OnXmlException(sGraphMLFileName, oXmlException); continue; } iGraphMLFiles++; } oBackgroundWorker.ReportProgress(0, String.Format( "Done. GraphML files imported: {0}." , iGraphMLFiles )); }