private void ShowProgressDialog(ProgressViewModel progressVM)
 {
     LaunchAction(() => this.dlgService.ShowDialog<ProgressDialog>(progressVM));
 }
        /// <summary>
        /// Generate C header files for a list of project items.
        /// </summary>
        /// <param name="projectItems">A list of project items.</param>
        private void ProcessFiles(IReadOnlyCollection<ProjectItem> projectItems)
        {
            bool showIncludeGuard;
            bool autoSaveFiles;
            var codeWriter = new CHeaderFileWriter();

            // Add Options
            using (var options = GetDialogPage(typeof(CSourceFileOptions)) as CSourceFileOptions)
            {
                codeWriter.HeaderComment = SetHeaderComment(options.HeaderComment);
                codeWriter.IncludeStaticFunctions = options.IncludeStaticFunctions;
                codeWriter.IncludeExternFunctions = options.IncludeExternFunctions;
                showIncludeGuard = options.ShowIncludeGuard;
                autoSaveFiles = options.AutoSaveFiles;
            }

            // Initialize viewmodel to keep track of progress
            using (var progressVM = new ProgressViewModel
                {
                    Minimum = 0.0,
                    Maximum = 1.0,
                    Message = "Starting...",
                    ProgressValue = 0.0
                })
            {
                this.ShowProgressDialog(progressVM);

                int i = 0;
                foreach (var projectItem in projectItems)
                {
                    if (projectItem.Document != null && !projectItem.Document.Saved && autoSaveFiles)
                        projectItem.Document.Save();

                    string file = projectItem.FileNames[0];
                    string itemToAdd = GetItemFileName(file);
                    bool error = false;

                    try
                    {
                        // Parse the file
                        log.Info("Processing {0}/{1}: {2}", ++i, projectItems.Count, file);
                        progressVM.Message = string.Format("{0}/{1}: Processing {2}", i, projectItems.Count, file);
                        if (!ParseItem(file, itemToAdd, codeWriter, showIncludeGuard, projectItem))
                            break;
                        progressVM.ProgressValue = Convert.ToDouble(i);
                    }
                    catch (ParserException tex)
                    {
                        // Go to file/line where the error occurred and display a dialog with the error message.
                        var window = ApplicationObject.ItemOperations.OpenFile(file);
                        window.Activate();
                        if (tex.LineNumber > 0)
                        {
                            var textSelection = window.Selection as TextSelection;
                            if (textSelection != null)
                                textSelection.GotoLine(tex.LineNumber, true);
                        }
                        log.Error(string.Format("Failed to parse file: {0}", file), tex);
                        this.ShowExceptionDialog(tex, string.Format("Failed to parse file: {0}", file));
                        error = true;
                    }
                    catch (Exception ex)
                    {
                        // Show a dialog with a less-than-helpful exception message.
                        log.Error(string.Format("Unknown error while parsing file: {0}", file), ex);
                        this.ShowExceptionDialog(ex, string.Format("Unknown exception while parsing: {0}", file));
                        error = true;
                    }
                    finally
                    {
                        // Log the result of the parse operation.
                        var messageBuilder = new System.Text.StringBuilder();
                        messageBuilder.AppendFormat("Completed processing file {0}/{1}: {2}.", i, projectItems.Count, file);
                        if (error)
                            messageBuilder.Append(" There were one or more errors detected during processing.");
                        else
                            messageBuilder.Append(" The operation was completed successfully.");
                        log.Info(messageBuilder.ToString());
                    }
                }
            }
        }