From fa9459cd2bd333541564d08a3bfc7d0edf44443b Mon Sep 17 00:00:00 2001 From: sr55 Date: Sat, 16 Aug 2008 19:42:44 +0000 Subject: WinGui: - One elusive cross-thread bug in the Activity window fix (hopefully) + some tweaks to the log display - Nicer Exception Handling Message box errors for frmReadDVD. - frmMain Minimize to taskbar no tooltip text exception fixed. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@1634 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- win/C#/Functions/SystemInfo.cs | 12 ++++ win/C#/frmActivityWindow.cs | 130 ++++++++++++++++------------------------- win/C#/frmMain.cs | 5 +- win/C#/frmReadDVD.cs | 89 ++++++++++++++++------------ 4 files changed, 118 insertions(+), 118 deletions(-) diff --git a/win/C#/Functions/SystemInfo.cs b/win/C#/Functions/SystemInfo.cs index c081e4f81..535bda6d3 100644 --- a/win/C#/Functions/SystemInfo.cs +++ b/win/C#/Functions/SystemInfo.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using Microsoft.Win32; namespace Handbrake.Functions { @@ -41,5 +42,16 @@ namespace Handbrake.Functions } #endregion + public Object getCpuCount() + { + RegistryKey RegKey = Registry.LocalMachine; + RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); + return RegKey.GetValue("ProcessorNameString"); + } + + public System.Windows.Forms.Screen screenBounds() + { + return System.Windows.Forms.Screen.PrimaryScreen; + } } } diff --git a/win/C#/frmActivityWindow.cs b/win/C#/frmActivityWindow.cs index 63157909c..fad0efcad 100644 --- a/win/C#/frmActivityWindow.cs +++ b/win/C#/frmActivityWindow.cs @@ -15,19 +15,17 @@ using System.IO; using System.Threading; using System.Diagnostics; using System.Runtime.InteropServices; -using Microsoft.Win32; + namespace Handbrake { public partial class frmActivityWindow : Form { - - Thread monitorFile; String read_file; + Thread monitor; frmMain mainWindow; frmQueue queueWindow; int position = 0; // Position in the arraylist reached by the current log output in the rtf box. - /// /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode. @@ -36,61 +34,46 @@ namespace Handbrake public frmActivityWindow(string file, frmMain fm, frmQueue fq) { InitializeComponent(); + this.rtf_actLog.Text = string.Empty; mainWindow = fm; queueWindow = fq; read_file = file; - - // Reset some varibles - this.rtf_actLog.Text = string.Empty; position = 0; + // System Information + Functions.SystemInfo info = new Functions.SystemInfo(); + + // Add a header to the log file indicating that it's from the Windows GUI and display the windows version + rtf_actLog.AppendText("### Windows GUI \n"); + rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString())); + rtf_actLog.AppendText(String.Format("### CPU: {0} \n", info.getCpuCount())); + rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", info.TotalPhysicalMemory())); + rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", info.screenBounds().Bounds.Width, info.screenBounds().Bounds.Height)); + rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath())); + rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath)); + rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath)); + rtf_actLog.AppendText("#########################################\n\n"); + string logFile = Path.Combine(Path.GetTempPath(), read_file); if (File.Exists(logFile)) { - - // Get the CPU Processor Name - RegistryKey RegKey = Registry.LocalMachine; - RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); - Object cpuType = RegKey.GetValue("ProcessorNameString"); - - // Get the screen resolution - System.Windows.Forms.Screen scr = System.Windows.Forms.Screen.PrimaryScreen; - - // Physical Ram - Functions.SystemInfo info = new Functions.SystemInfo(); - uint memory = info.TotalPhysicalMemory(); - - // Add a header to the log file indicating that it's from the Windows GUI and display the windows version - rtf_actLog.AppendText("### Windows GUI \n"); - rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString())); - rtf_actLog.AppendText(String.Format("### CPU: {0} \n", cpuType)); - rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", memory)); - rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", scr.Bounds.Width, scr.Bounds.Height)); - rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath())); - rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath)); - rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n###\n", Application.UserAppDataPath)); - // Start a new thread to run the autoUpdate process - monitorFile = new Thread(autoUpdate); - monitorFile.Start(); + monitor = new Thread(autoUpdate); + monitor.IsBackground = true; + monitor.Start(); } else - MessageBox.Show("The log file could not be found. Maybe you cleared your system's tempory folder or maybe you just havn't run an encode yet.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Warning); - - // Handle the event of the window being disposed. This is needed to make sure HandBrake exit's cleanly. + 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); + + // When the window closes, we want to abort the monitor thread. this.Disposed += new EventHandler(forceQuit); } - // Ok, so, this function is called when someone closes frmMain but didn't close frmActivitWindow first. - // When you close frmMain, the activity window gets closed (disposed of) but, this doens't kill the threads that it started. - // When that thread tries to access the disposed rich text box, it causes an exception. - // Basically, this function is called when the window is disposed of, to kill the thread and close the window properly. - // This allows HandBrake to close cleanly. private void forceQuit(object sender, EventArgs e) { - if (monitorFile != null) - monitorFile.Abort(); + if (monitor != null) + monitor.Abort(); this.Close(); } @@ -101,7 +84,7 @@ namespace Handbrake Boolean lastUpdate = false; updateTextFromThread(); while (true) - { + { if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true)) updateTextFromThread(); else @@ -110,42 +93,42 @@ namespace Handbrake if (lastUpdate == false) updateTextFromThread(); - lastUpdate = true; - position = 0; + lastUpdate = true; // Prevents the log window from being updated when there is no encode going. + position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused } Thread.Sleep(5000); } } - private delegate void UpdateUIHandler(); private void updateTextFromThread() { - try - { - if (this.InvokeRequired) - { - this.BeginInvoke(new UpdateUIHandler(updateTextFromThread)); - return; - } - // Initialize a pointer and get the log data arraylist - ArrayList data = readFile(); + string text = ""; + ArrayList data = readFile(); - while (position < data.Count) - { - rtf_actLog.AppendText(data[position].ToString()); - if (data[position].ToString().Contains("has exited")) - { - rtf_actLog.AppendText("\n ############ End of Encode ############## \n"); - } - position++; - } + while (position < data.Count) + { + text = data[position].ToString(); + if (data[position].ToString().Contains("has exited")) + text = "\n ############ End of Encode ############## \n"; + position++; - // this.rtf_actLog.SelectionStart = this.rtf_actLog.Text.Length - 1; - // this.rtf_actLog.ScrollToCaret(); + SetText(text); } - catch (Exception exc) + } + delegate void SetTextCallback(string text); + private void SetText(string text) + { + // InvokeRequired required compares the thread ID of the + // calling thread to the thread ID of the creating thread. + // If these threads are different, it returns true. + if (this.rtf_actLog.InvokeRequired) + { + SetTextCallback d = new SetTextCallback(SetText); + this.Invoke(d, new object[] { text }); + } + else { - MessageBox.Show("An error has occured in: updateTextFromThread(). \n You may have to restart HandBrake. \n Error Information: \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + this.rtf_actLog.AppendText(text); } } @@ -191,16 +174,5 @@ namespace Handbrake return null; } - - // Ok, We need to make sure the monitor thread is dead when we close the window. - protected override void OnClosing(CancelEventArgs e) - { - if (monitorFile != null) - monitorFile.Abort(); - e.Cancel = true; - this.Hide(); - base.OnClosing(e); - } - } } \ No newline at end of file diff --git a/win/C#/frmMain.cs b/win/C#/frmMain.cs index 4c533ea43..16d9c54e4 100644 --- a/win/C#/frmMain.cs +++ b/win/C#/frmMain.cs @@ -1879,7 +1879,10 @@ namespace Handbrake if (FormWindowState.Minimized == this.WindowState) { notifyIcon.Visible = true; - notifyIcon.BalloonTipText = lbl_encode.Text; + if (lbl_encode.Text != "") + notifyIcon.BalloonTipText = lbl_encode.Text; + else + notifyIcon.BalloonTipText = "Not Encoding"; notifyIcon.ShowBalloonTip(500); this.Hide(); } diff --git a/win/C#/frmReadDVD.cs b/win/C#/frmReadDVD.cs index 3eb212d4f..21dfa6505 100644 --- a/win/C#/frmReadDVD.cs +++ b/win/C#/frmReadDVD.cs @@ -27,6 +27,7 @@ namespace Handbrake private delegate void UpdateUIHandler(); Process hbproc; Functions.Common hb_common_func = new Functions.Common(); + Functions.Encode process = new Functions.Encode(); public frmReadDVD(string inputFile, frmMain parent) { @@ -45,10 +46,51 @@ namespace Handbrake } catch (Exception exc) { - MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString()); + MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + private void startProc(object state) + { + try + { + string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); + string dvdInfoPath = Path.Combine(Path.GetTempPath(), "dvdinfo.dat"); + + // Make we don't pick up a stale hb_encode_log.dat (and that we have rights to the file) + if (File.Exists(dvdInfoPath)) + File.Delete(dvdInfoPath); + + string strCmdLine = String.Format(@"cmd /c """"{0}"" -i ""{1}"" -t0 -v >""{2}"" 2>&1""", handbrakeCLIPath, inputFile, dvdInfoPath); + + ProcessStartInfo hbParseDvd = new ProcessStartInfo("CMD.exe", strCmdLine); + hbParseDvd.WindowStyle = ProcessWindowStyle.Hidden; + + using (hbproc = Process.Start(hbParseDvd)) + { + hbproc.WaitForExit(); + } + + if (!File.Exists(dvdInfoPath)) + { + throw new Exception("Unable to retrieve the DVD Info. dvdinfo.dat is missing. \nExpected location of dvdinfo.dat: \n" + dvdInfoPath); + } + + using (StreamReader sr = new StreamReader(dvdInfoPath)) + { + thisDvd = Parsing.DVD.Parse(sr); + sr.Close(); + sr.Dispose(); + } + + updateUIElements(); + } + catch (Exception exc) + { + MessageBox.Show("frmReadDVD.cs - startProc() " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + closeWindowAfterError(); + } + } private void updateUIElements() { try @@ -76,54 +118,25 @@ namespace Handbrake } catch (Exception exc) { - MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString()); + MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); } } - - Functions.Encode process = new Functions.Encode(); - - private void startProc(object state) + private void closeWindowAfterError() { try { - string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); - string dvdInfoPath = Path.Combine(Path.GetTempPath(), "dvdinfo.dat"); - - // Make we don't pick up a stale hb_encode_log.dat (and that we have rights to the file) - if (File.Exists(dvdInfoPath)) - File.Delete(dvdInfoPath); - - string strCmdLine = String.Format(@"cmd /c """"{0}"" -i ""{1}"" -t0 -v >""{2}"" 2>&1""", handbrakeCLIPath, inputFile, dvdInfoPath); - - ProcessStartInfo hbParseDvd = new ProcessStartInfo("CMD.exe", strCmdLine); - hbParseDvd.WindowStyle = ProcessWindowStyle.Hidden; - - using (hbproc = Process.Start(hbParseDvd)) - { - hbproc.WaitForExit(); - } - - if (!File.Exists(dvdInfoPath)) - { - throw new Exception("Unable to retrieve the DVD Info. dvdinfo.dat is missing."); - } - - using (StreamReader sr = new StreamReader(dvdInfoPath)) + if (this.InvokeRequired) { - thisDvd = Parsing.DVD.Parse(sr); - sr.Close(); - sr.Dispose(); + this.BeginInvoke(new UpdateUIHandler(closeWindowAfterError)); + return; } - - updateUIElements(); + this.Close(); } catch (Exception exc) { - MessageBox.Show("frmReadDVD.cs - startProc " + exc.ToString()); - this.Close(); + MessageBox.Show("frmReadDVD.cs - closeWindowAfterError - Unable to recover from a serious error. \n\n Error Information: \n " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } - } private void btn_cancel_Click(object sender, EventArgs e) @@ -149,7 +162,7 @@ namespace Handbrake } catch (Exception ex) { - MessageBox.Show(ex.Message); + MessageBox.Show("Unable to kill HandBrakeCLI.exe \n\nError Information: \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } -- cgit v1.2.3