Exemplo n.º 1
0
        public async Task <IWordDocument> LoadAsync(string filePath)
        {
            nat.Application app = null;
            nat.Document    doc = null;

            try
            {
                app = new nat.Application();
                doc = app.Documents.Open(filePath);

                IReadOnlyCollection <IExternalHyperLink> externalLinks = null;
                IReadOnlyCollection <IWord>  words  = null;
                IReadOnlyCollection <ITable> tables = null;
                IReadOnlyCollection <IShape> shapes = null;
                IListOfShapes listOfShapes          = null;
                IListOfTables listOfTables          = null;
                ISections     sections = null;

                var tasks = new List <Task>
                {
                    Task.Run(
                        async() =>
                    {
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } starting words");
                        words = await _wordFactory.CreateAllAsync(doc);
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } finished words");
                    }),
                    Task.Run(
                        async() =>
                    {
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } starting externalLinks");
                        externalLinks = await _externalHyperLinkFactory.CreateAllAsync(doc);
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } finished externalLinks");
                    }),
                    Task.Run(
                        async() =>
                    {
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } starting tables");
                        tables = await _tableFactory.CreateAllAsync(doc);
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } finished tables");
                    }),
                    Task.Run(
                        async() =>
                    {
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } starting shapes");
                        shapes = await _shapeFactory.CreateAllAsync(doc);
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } finished shapes");
                    }),
                    Task.Run(
                        async() =>
                    {
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } starting listOfShapes");
                        listOfShapes = await _listFactory.CreateListOfShapesAsync(doc);
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } finished listOfShapes");
                    }),
                    Task.Run(
                        async() =>
                    {
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } starting listOfTables");
                        listOfTables = await _listFactory.CreateListOfTablesAsync(doc);
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } finished listOfTables");
                    }),
                    Task.Run(
                        async() =>
                    {
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } starting sections");
                        sections = await _sectionsFactory.CreateAsync(doc);
                        Console.WriteLine($"{ DateTime.Now.ToLongTimeString() } finished sections");
                    })
                };

                await Task.WhenAll(tasks);

                IWordDocument result = new WordDocument(
                    externalLinks,
                    words,
                    tables,
                    shapes,
                    listOfShapes,
                    listOfTables,
                    sections);

                return(result);
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                    Marshal.ReleaseComObject(doc);
                }

                if (app != null)
                {
                    app.Quit();
                    Marshal.ReleaseComObject(app);
                }

                _wordKiller.KillAllInstances();
            }
        }