コード例 #1
0
        /// <summary>
        /// Time consuming operations go here </br>
        /// i.e. Database operations,Reporting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            //Load XML file into preview
            XmlDocument XMLdoc = new XmlDocument();

            try
            {
                XMLdoc.Load(readyToRenameLog);
            }
            catch (XmlException)
            {
                System.Windows.Forms.MessageBox.Show("The XML file is invalid. Check for error logs.");
                return;
            }

            if (XMLdoc.HasChildNodes)
            {
                var xmlEntities = new List <XmlEntity>();

                int fileCounter = 0;
                //Rename Files
                foreach (XmlNode item in XMLdoc.ChildNodes)
                {
                    try
                    {
                        //REM Thread.Sleep(1);

                        GetChildren(item, "File");

                        // Periodically report progress to the main thread so that it can
                        // update the UI.  In most cases you'll just need to send an
                        // integer that will update a ProgressBar
                        m_oWorker.ReportProgress(fileCounter / XMLdoc.ChildNodes.Count);
                    }
                    catch (Exception exFile)
                    {
                        LogError(ErrorLogName, exFile.Message.ToString());
                    }
                }

                //Rename Folders
                foreach (XmlNode item in XMLdoc.ChildNodes)
                {
                    try
                    {
                        //REM  Thread.Sleep(1);
                        GetChildren(item, "Directory");
                    }
                    catch (Exception exDir)
                    {
                        LogError(ErrorLogName, exDir.Message.ToString());
                    }
                }

                //Success
                AutoClosingMessageBox.Show("Renamed all files and folders. Check for error logs.", "Success!", 3000);
                //MessageBoxResult result2 = System.Windows.MessageBox.Show("Renamed all files and folders. Check for error logs.", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
            }

            //XML has no child nodes

            // Periodically check if a cancellation request is pending.
            // If the user clicks cancel the line
            // m_AsyncWorker.CancelAsync(); if ran above.  This
            // sets the CancellationPending to true.
            // You must check this flag in here and react to it.
            // We react to it by setting e.Cancel to true and leaving
            if (m_oWorker.CancellationPending)
            {
                // Set the e.Cancel flag so that the WorkerCompleted event
                // knows that the process was cancelled.
                e.Cancel = true;
                m_oWorker.ReportProgress(0);
                return;
            }

            //Report 100% completion on operation completed
            m_oWorker.ReportProgress(100);
        }
コード例 #2
0
        private void ShowPreview(List <string> theList)
        {
            thePathTxtText = thePathTxt.Text;
            string LogName = "Test_" + DateTime.Now.Year + "_" + DateTime.Now.DayOfWeek + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second;

            ErrorLogName     = LogName + "_ERROR" + ".xml";
            LogName          = LogName + ".xml";
            readyToRenameLog = LogName;
            readyList        = new List <string>();
            readyList        = theList;
            //Loop recursively through all files and folders

            m_oWorker2 = new BackgroundWorker();

            // Create a background worker thread that ReportsProgress &
            // SupportsCancellation
            // Hook up the appropriate events.
            m_oWorker2.DoWork          += new DoWorkEventHandler(m_oWorker2_DoWork);
            m_oWorker2.ProgressChanged += new ProgressChangedEventHandler
                                              (m_oWorker2_ProgressChanged);
            m_oWorker2.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                                                 (m_oWorker2_RunWorkerCompleted);
            m_oWorker2.WorkerReportsProgress      = true;
            m_oWorker2.WorkerSupportsCancellation = true;

            cancelBtn.Visibility = Visibility.Visible;
            cancelBtn.IsEnabled  = true;

            m_oWorker2.RunWorkerAsync();
            resetEvent.WaitOne();

            if (processResult == true)
            {
                //Load XML file into preview
                XmlDocument XMLdoc         = new XmlDocument();
                string      LogNameTrimmed = readyToRenameLog.Substring(0, readyToRenameLog.LastIndexOf("_"));
                var         currentPath    = new DirectoryInfo(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

                var myFile = (from f in currentPath.GetFiles() orderby f.LastWriteTime descending select f).First();

                if (!File.Exists(readyToRenameLog) && myFile.Exists)
                {
                    if (myFile.LastWriteTime > DateTime.Now.AddSeconds(-2))
                    {
                        readyToRenameLog = myFile.Name;
                    }
                }

                if (File.Exists(readyToRenameLog))
                {
                    // Create a background worker thread that ReportsProgress &
                    // SupportsCancellation
                    // Hook up the appropriate events.
                    //m_oWorker3.DoWork += new DoWorkEventHandler(m_oWorker3_DoWork);
                    //m_oWorker3.ProgressChanged += new ProgressChangedEventHandler
                    //        (m_oWorker3_ProgressChanged);
                    //m_oWorker3.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                    //        (m_oWorker3_RunWorkerCompleted);
                    //m_oWorker3.WorkerReportsProgress = true;
                    //m_oWorker3.WorkerSupportsCancellation = true;

                    //cancelBtn.Visibility = Visibility.Visible;
                    //cancelBtn.IsEnabled = true;

                    //m_oWorker3.RunWorkerAsync();

                    try
                    {
                        XMLdoc.Load(readyToRenameLog);
                    }
                    catch (XmlException)
                    {
                        MessageBoxResult result2 = System.Windows.MessageBox.Show("XML is invalid. Check for error logs.", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }

                    Viewer vXMLViewer = new Viewer();
                    vXMLViewer.xmlDocument = XMLdoc;

                    Window window = new Window
                    {
                        Title   = "Preview Renames",
                        Width   = 800,
                        Height  = 600,
                        Content = vXMLViewer
                    };

                    window.ShowDialog();


                    //Enable Rename button
                    renameBtn.IsEnabled = true;
                }
                else
                {
                    AutoClosingMessageBox.Show("Nothing to rename, all clean.", "Success!", 3000);
                    //MessageBoxResult result2 = System.Windows.MessageBox.Show("Nothing to rename, all clean.", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
                    renameBtn.IsEnabled = false;
                }
            }
            else
            {
                //Do Nothing
            }

            //bool processResult = ProcessDir(thePathTxt.Text, readyToRenameLog, theList);

            //if (processResult == true)
            //{


            //    //Load XML file into preview
            //    XmlDocument XMLdoc = new XmlDocument();
            //    string LogNameTrimmed = LogName.Substring(0, LogName.LastIndexOf("_"));
            //    var currentPath = new DirectoryInfo(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            //    var myFile = (from f in currentPath.GetFiles() orderby f.LastWriteTime descending select f).First();

            //    if (!File.Exists(LogName) && myFile.Exists)
            //    {
            //        if (myFile.LastWriteTime > DateTime.Now.AddSeconds(-2))
            //        {
            //            LogName = myFile.Name;
            //            readyToRenameLog = LogName;
            //        }
            //    }

            //    if (File.Exists(LogName))
            //    {
            //        try
            //        {
            //            XMLdoc.Load(LogName);
            //        }
            //        catch (XmlException)
            //        {
            //            MessageBoxResult result2 = System.Windows.MessageBox.Show("XML is invalid. Check for error logs.", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);

            //        }

            //        Viewer vXMLViewer = new Viewer();
            //        vXMLViewer.xmlDocument = XMLdoc;

            //        Window window = new Window
            //        {
            //            Title = "Preview Renames",
            //            Width = 800,
            //            Height = 600,
            //            Content = vXMLViewer

            //        };

            //        window.ShowDialog();


            //        //Enable Rename button
            //        renameBtn.IsEnabled = true;
            //    }
            //    else
            //    {
            //        MessageBoxResult result2 = System.Windows.MessageBox.Show("Nothing to rename, all clean.", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
            //        renameBtn.IsEnabled = false;

            //    }

            //}
            //else
            //{
            //    //Do Nothing
            //}
        }