]> granicus.if.org Git - handbrake/commitdiff
WinGui:
authorsr55 <sr55.hb@outlook.com>
Wed, 12 Nov 2008 22:38:38 +0000 (22:38 +0000)
committersr55 <sr55.hb@outlook.com>
Wed, 12 Nov 2008 22:38:38 +0000 (22:38 +0000)
- Fix a potential crash in the Activity window where the window handler may not be initialized in time for the function setText to use it.

git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@1918 b64f7644-9d1e-0410-96f1-a4d463321fa5

win/C#/frmActivityWindow.cs

index 17ec856a8533ace3adc8afef5316c123003864bb..5508d0b93b1f2ded871bba86b34d452954c2085f 100644 (file)
@@ -37,6 +37,9 @@ namespace Handbrake
             InitializeComponent();\r
             this.rtf_actLog.Text = string.Empty;\r
 \r
+            // When the window closes, we want to abort the monitor thread.\r
+            this.Disposed += new EventHandler(forceQuit);\r
+\r
             mainWindow = fm;\r
             queueWindow = fq;\r
             read_file = file;\r
@@ -45,17 +48,13 @@ namespace Handbrake
             // Print the Log header in the Rich text box.\r
             displayLogHeader();\r
 \r
-            // Start a new thread which will montior and keep the log window up to date if required/\r
-            startLogThread(read_file);\r
-\r
             if (file == "dvdinfo.dat")\r
                 txt_log.Text = "Scan Log";\r
             else if (file == "hb_encode_log.dat")\r
                 txt_log.Text = "Encode Log";\r
 \r
-\r
-            // When the window closes, we want to abort the monitor thread.\r
-            this.Disposed += new EventHandler(forceQuit);\r
+            // Start a new thread which will montior and keep the log window up to date if required/\r
+            startLogThread(read_file);            \r
         }\r
 \r
         /// <summary>\r
@@ -84,16 +83,24 @@ namespace Handbrake
         /// <param name="file"> File which will be used to populate the Rich text box.</param>\r
         private void startLogThread(string file)\r
         {\r
-            string logFile = Path.Combine(Path.GetTempPath(), file);\r
-            if (File.Exists(logFile))\r
+            try\r
             {\r
-                // Start a new thread to run the autoUpdate process\r
-                monitor = new Thread(autoUpdate);\r
-                monitor.IsBackground = true;\r
-                monitor.Start();\r
+                string logFile = Path.Combine(Path.GetTempPath(), file);\r
+                if (File.Exists(logFile))\r
+                {\r
+                    // Start a new thread to run the autoUpdate process\r
+                    monitor = new Thread(autoUpdate);\r
+                    monitor.IsBackground = true;\r
+                    monitor.Start();\r
+                }\r
+                else\r
+                    rtf_actLog.AppendText("\n\n\nERROR: The log file could not be found. \nMaybe you cleared your system's tempory folder or maybe you just havn't run an encode yet. \nTried to find the log file in: " + logFile);\r
+\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                MessageBox.Show("startLogThread(): Exception: \n" + exc);\r
             }\r
-            else\r
-                rtf_actLog.AppendText("\n\n\nERROR: The log file could not be found. \nMaybe you cleared your system's tempory folder or maybe you just havn't run an encode yet. \nTried to find the log file in: " + logFile);\r
         }\r
 \r
         /// <summary>\r
@@ -151,22 +158,34 @@ namespace Handbrake
         /// <param name="state"></param>\r
         private void autoUpdate(object state)\r
         {\r
-            Boolean lastUpdate = false;\r
-            updateTextFromThread();\r
-            while (true)\r
+            try\r
             {\r
-                if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))\r
-                    updateTextFromThread();\r
-                else\r
+                Boolean lastUpdate = false;\r
+                updateTextFromThread();\r
+                while (true)\r
                 {\r
-                    // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
-                    if (lastUpdate == false)\r
+                    if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))\r
                         updateTextFromThread();\r
-\r
-                    lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
-                    position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
+                    else\r
+                    {\r
+                        // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
+                        if (lastUpdate == false)\r
+                            updateTextFromThread();\r
+\r
+                        lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
+                        position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
+                    }\r
+                    Thread.Sleep(5000);\r
                 }\r
-                Thread.Sleep(5000);\r
+            }\r
+            catch (ThreadAbortException)\r
+            {\r
+                // Do Nothing. This is needed since we run thread.abort(). \r
+                // Should probably find a better way of making this work at some point.\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                MessageBox.Show("autoUpdate(): Exception: \n" + exc);\r
             }\r
         }\r
 \r
@@ -175,18 +194,25 @@ namespace Handbrake
         /// </summary>\r
         private void updateTextFromThread()\r
         {\r
-            string text = "";\r
-            List<string> data = readFile();\r
-            int count = data.Count;\r
-\r
-            while (position < count)\r
+            try\r
             {\r
-                text = data[position].ToString();\r
-                if (data[position].ToString().Contains("has exited"))\r
-                    text = "\n ############ End of Log ############## \n";\r
-                position++;\r
+                string text = "";\r
+                List<string> data = readFile();\r
+                int count = data.Count;\r
+\r
+                while (position < count)\r
+                {\r
+                    text = data[position].ToString();\r
+                    if (data[position].ToString().Contains("has exited"))\r
+                        text = "\n ############ End of Log ############## \n";\r
+                    position++;\r
 \r
-                SetText(text);\r
+                    SetText(text);\r
+                }\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                MessageBox.Show("updateTextFromThread(): Exception: \n" + exc);\r
             }\r
         }\r
 \r
@@ -196,17 +222,25 @@ namespace Handbrake
         /// <param name="text"></param>\r
         private void SetText(string text)\r
         {\r
-            // InvokeRequired required compares the thread ID of the\r
-            // calling thread to the thread ID of the creating thread.\r
-            // If these threads are different, it returns true.\r
-            if (this.rtf_actLog.InvokeRequired)\r
+            try\r
             {\r
-                SetTextCallback d = new SetTextCallback(SetText);\r
-                this.Invoke(d, new object[] { text });\r
+                // InvokeRequired required compares the thread ID of the\r
+                // calling thread to the thread ID of the creating thread.\r
+                // If these threads are different, it returns true.\r
+                if (this.IsHandleCreated) // Make sure the windows has a handle before doing anything\r
+                {\r
+                    if (this.rtf_actLog.InvokeRequired)\r
+                    {\r
+                        SetTextCallback d = new SetTextCallback(SetText);\r
+                        this.Invoke(d, new object[] { text });\r
+                    }\r
+                    else\r
+                        this.rtf_actLog.AppendText(text);\r
+                }\r
             }\r
-            else\r
+            catch (Exception exc)\r
             {\r
-                this.rtf_actLog.AppendText(text);\r
+                MessageBox.Show("SetText(): Exception: \n" + exc);\r
             }\r
         }\r
 \r
@@ -264,7 +298,10 @@ namespace Handbrake
         private void forceQuit(object sender, EventArgs e)\r
         {\r
             if (monitor != null)\r
-                monitor.Abort();\r
+            {\r
+                while (monitor.IsAlive)\r
+                    monitor.Abort();\r
+            }\r
 \r
             this.Close();\r
         }\r