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.Collections.Generic;
\r
10 using System.ComponentModel;
\r
12 using System.Drawing;
\r
14 using System.Windows.Forms;
\r
16 using System.Threading;
\r
17 using System.Diagnostics;
\r
18 using System.Runtime.InteropServices;
\r
23 public partial class frmActivityWindow : Form
\r
28 frmQueue queueWindow;
\r
29 int position = 0; // Position in the arraylist reached by the current log output in the rtf box.
\r
32 /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.
\r
35 public frmActivityWindow(string file, frmMain fm, frmQueue fq)
\r
37 InitializeComponent();
\r
38 this.rtf_actLog.Text = string.Empty;
\r
45 // System Information
\r
46 Functions.SystemInfo info = new Functions.SystemInfo();
\r
48 // Add a header to the log file indicating that it's from the Windows GUI and display the windows version
\r
49 rtf_actLog.AppendText("### Windows GUI \n");
\r
50 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString()));
\r
51 rtf_actLog.AppendText(String.Format("### CPU: {0} \n", info.getCpuCount()));
\r
52 rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", info.TotalPhysicalMemory()));
\r
53 rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", info.screenBounds().Bounds.Width, info.screenBounds().Bounds.Height));
\r
54 rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
55 rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
56 rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
57 rtf_actLog.AppendText("#########################################\n\n");
\r
59 string logFile = Path.Combine(Path.GetTempPath(), read_file);
\r
60 if (File.Exists(logFile))
\r
62 // Start a new thread to run the autoUpdate process
\r
63 monitor = new Thread(autoUpdate);
\r
64 monitor.IsBackground = true;
\r
68 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
70 // When the window closes, we want to abort the monitor thread.
\r
71 this.Disposed += new EventHandler(forceQuit);
\r
74 private void forceQuit(object sender, EventArgs e)
\r
76 if (monitor != null)
\r
82 // Update the Activity window every 5 seconds with the latest log data.
\r
83 private void autoUpdate(object state)
\r
85 Boolean lastUpdate = false;
\r
86 updateTextFromThread();
\r
89 if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))
\r
90 updateTextFromThread();
\r
93 // The encode may just have stoped, so, refresh the log one more time before restarting it.
\r
94 if (lastUpdate == false)
\r
95 updateTextFromThread();
\r
97 lastUpdate = true; // Prevents the log window from being updated when there is no encode going.
\r
98 position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused
\r
100 Thread.Sleep(5000);
\r
104 private void updateTextFromThread()
\r
107 List<string> data = readFile();
\r
108 int count = data.Count;
\r
110 while (position < count)
\r
112 text = data[position].ToString();
\r
113 if (data[position].ToString().Contains("has exited"))
\r
114 text = "\n ############ End of Encode ############## \n";
\r
120 delegate void SetTextCallback(string text);
\r
121 private void SetText(string text)
\r
123 // InvokeRequired required compares the thread ID of the
\r
124 // calling thread to the thread ID of the creating thread.
\r
125 // If these threads are different, it returns true.
\r
126 if (this.rtf_actLog.InvokeRequired)
\r
128 SetTextCallback d = new SetTextCallback(SetText);
\r
129 this.Invoke(d, new object[] { text });
\r
133 this.rtf_actLog.AppendText(text);
\r
137 private List<string> readFile()
\r
139 // Ok, the task here is to, Get an arraylist of log data.
\r
140 // And update some global varibles which are pointers to the last displayed log line.
\r
141 List<string> logData = new List<string>();
\r
145 // 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
146 // we'll need to make a copy of it.
\r
147 string logFile = Path.Combine(Path.GetTempPath(), read_file);
\r
148 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");
\r
150 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
151 if (File.Exists(logFile2))
\r
152 File.Delete(logFile2);
\r
154 // Copy the log file.
\r
155 File.Copy(logFile, logFile2);
\r
157 // Open the copied log file for reading
\r
158 StreamReader sr = new StreamReader(logFile2);
\r
159 string line = sr.ReadLine();
\r
160 while (line != null)
\r
162 if (line.Trim() != "")
\r
163 logData.Add(line + System.Environment.NewLine);
\r
165 line = sr.ReadLine();
\r
172 catch (Exception exc)
\r
174 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