1 /* frmActivityWindow.cs $
\r
3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
8 using System.Collections;
\r
9 using System.ComponentModel;
\r
11 using System.Drawing;
\r
13 using System.Windows.Forms;
\r
15 using System.Threading;
\r
16 using System.Diagnostics;
\r
17 using System.Runtime.InteropServices;
\r
23 public partial class frmActivityWindow : Form
\r
29 frmQueue queueWindow;
\r
30 int position = 0; // Position in the arraylist reached by the current log output in the rtf box.
\r
34 /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.
\r
37 public frmActivityWindow(string file, frmMain fm, frmQueue fq)
\r
39 InitializeComponent();
\r
45 // Reset some varibles
\r
46 this.rtf_actLog.Text = string.Empty;
\r
49 string logFile = Path.Combine(Path.GetTempPath(), read_file);
\r
50 if (File.Exists(logFile))
\r
52 // Add a header to the log file indicating that it's from the Windows GUI and display the windows version
\r
53 rtf_actLog.AppendText("### Windows GUI \n");
\r
54 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString()));
\r
56 // Start a new thread to run the autoUpdate process
\r
57 monitorFile = new Thread(autoUpdate);
\r
58 monitorFile.Start();
\r
61 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);
\r
63 // Handle the event of the window being disposed. This is needed to make sure HandBrake exit's cleanly.
\r
64 this.Disposed += new EventHandler(forceQuit);
\r
67 // Ok, so, this function is called when someone closes frmMain but didn't close frmActivitWindow first.
\r
68 // When you close frmMain, the activity window gets closed (disposed of) but, this doens't kill the threads that it started.
\r
69 // When that thread tries to access the disposed rich text box, it causes an exception.
\r
70 // Basically, this function is called when the window is disposed of, to kill the thread and close the window properly.
\r
71 // This allows HandBrake to close cleanly.
\r
72 private void forceQuit(object sender, EventArgs e)
\r
74 if (monitorFile != null)
\r
75 monitorFile.Abort();
\r
80 // Update the Activity window every 5 seconds with the latest log data.
\r
81 private void autoUpdate(object state)
\r
83 Boolean lastUpdate = false;
\r
84 updateTextFromThread();
\r
87 if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))
\r
88 updateTextFromThread();
\r
91 // The encode may just have stoped, so, refresh the log one more time before restarting it.
\r
92 if (lastUpdate == false)
\r
93 updateTextFromThread();
\r
102 private delegate void UpdateUIHandler();
\r
103 private void updateTextFromThread()
\r
107 if (this.InvokeRequired)
\r
109 this.BeginInvoke(new UpdateUIHandler(updateTextFromThread));
\r
112 // Initialize a pointer and get the log data arraylist
\r
113 ArrayList data = readFile();
\r
115 while (position < data.Count)
\r
117 rtf_actLog.AppendText(data[position].ToString());
\r
118 if (data[position].ToString().Contains("has exited"))
\r
120 rtf_actLog.AppendText("\n ############ End of Encode ############## \n");
\r
125 // this.rtf_actLog.SelectionStart = this.rtf_actLog.Text.Length - 1;
\r
126 // this.rtf_actLog.ScrollToCaret();
\r
128 catch (Exception exc)
\r
130 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);
\r
134 private ArrayList readFile()
\r
136 // Ok, the task here is to, Get an arraylist of log data.
\r
137 // And update some global varibles which are pointers to the last displayed log line.
\r
138 ArrayList logData = new ArrayList();
\r
142 // hb_encode_log.dat is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it (Not even in read only mode),
\r
143 // we'll need to make a copy of it.
\r
144 string logFile = Path.Combine(Path.GetTempPath(), read_file);
\r
145 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");
\r
147 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
148 if (File.Exists(logFile2))
\r
149 File.Delete(logFile2);
\r
151 // Copy the log file.
\r
152 File.Copy(logFile, logFile2);
\r
154 // Open the copied log file for reading
\r
155 StreamReader sr = new StreamReader(logFile2);
\r
156 string line = sr.ReadLine();
\r
157 while (line != null)
\r
159 if (line.Trim() != "")
\r
160 logData.Add(line + System.Environment.NewLine);
\r
162 line = sr.ReadLine();
\r
169 catch (Exception exc)
\r
171 MessageBox.Show("Error in readFile() \n Unable to read the log file.\n You may have to restart HandBrake.\n Error Information: \n\n" + exc.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
177 // Ok, We need to make sure the monitor thread is dead when we close the window.
\r
178 protected override void OnClosing(CancelEventArgs e)
\r
180 if (monitorFile != null)
\r
181 monitorFile.Abort();
\r