X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=win%2FC%23%2FfrmActivityWindow.cs;h=9509500a61036066dc2b4ba7154f7912baf044cb;hb=b4464b6d962bb939a8f38b4d50057c441ab48111;hp=c09920126237b6d1325bdfff3d4517528214f0cc;hpb=e1d7d5a597b1323f3d9b4d1cc9a833c527b294ca;p=handbrake-jp%2Fhandbrake-jp-git.git diff --git a/win/C#/frmActivityWindow.cs b/win/C#/frmActivityWindow.cs index c0992012..9509500a 100644 --- a/win/C#/frmActivityWindow.cs +++ b/win/C#/frmActivityWindow.cs @@ -18,29 +18,38 @@ namespace Handbrake { public partial class frmActivityWindow : Form { - private delegate void SetTextCallback(string text); - private String read_file; - private Thread monitor; - private EncodeAndQueueHandler encodeQueue; - private int position; // Position in the arraylist reached by the current log output in the rtf box. - private string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; - private frmMain mainWin; - private Boolean lastUpdate; + private delegate void setTextCallback(string text); + private String _readFile; + private readonly EncodeAndQueueHandler _encodeQueue; + private int _position; // Position in the arraylist reached by the current log output in the rtf box. + private readonly frmMain _mainWin; + private Boolean _lastUpdate; + private Boolean fileNotFoundQuickFix; public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw) { InitializeComponent(); - encodeQueue = eh; - mainWin = mw; + _encodeQueue = eh; + _mainWin = mw; + + fileNotFoundQuickFix = false; if (file == "last_scan_log.txt") - setLogView(true); + SetLogView(true); else - setLogView(false); + SetLogView(false); // Start a new thread which will montior and keep the log window up to date if required/ - startLogThread(read_file); + try + { + Thread monitor = new Thread(AutoUpdate) { IsBackground = true }; + monitor.Start(); + } + catch (Exception exc) + { + MessageBox.Show("startLogThread(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } /// @@ -49,27 +58,13 @@ namespace Handbrake /// Encode = false; /// /// Boolean. Scan = true - public void setLogView(Boolean scan) + public void SetLogView(Boolean scan) { - position = 0; + // Reset + _position = 0; rtf_actLog.Text = String.Empty; - displayLogHeader(); - if (scan) - { - txt_log.Text = "Scan Log"; - read_file = "last_scan_log.txt"; - } - else - { - read_file = "last_encode_log.txt"; - txt_log.Text = "Encode Log"; - } - lastUpdate = false; - } - private void displayLogHeader() - { - // Add a header to the log file indicating that it's from the Windows GUI and display the windows version + // Print the log header rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version)); rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion)); rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount())); @@ -79,51 +74,46 @@ namespace Handbrake 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"); - if (encodeQueue.isEncoding && encodeQueue.LastEncode.Query != String.Empty) + if ((!_encodeQueue.LastEncode.IsEmpty) && _encodeQueue.LastEncode.Query != String.Empty) { - rtf_actLog.AppendText("### CLI Query: " + encodeQueue.LastEncode.Query + "\n\n"); + rtf_actLog.AppendText("### CLI Query: " + _encodeQueue.LastEncode.Query + "\n\n"); rtf_actLog.AppendText("#########################################\n\n"); } - } - private void startLogThread(string file) - { - try - { - string logFile = Path.Combine(logDir, file); - if (File.Exists(logFile)) - { - monitor = new Thread(autoUpdate) { IsBackground = true }; - monitor.Start(); - } - else - 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); + // Seutp the log file + if (scan) + { + txt_log.Text = "Scan Log"; + _readFile = "last_scan_log.txt"; } - catch (Exception exc) + else { - MessageBox.Show("startLogThread(): Exception: \n" + exc); + _readFile = "last_encode_log.txt"; + txt_log.Text = "Encode Log"; } + _lastUpdate = false; } - private void autoUpdate(object state) + + private void AutoUpdate(object state) { try { - lastUpdate = false; - updateTextFromThread(); + _lastUpdate = false; + UpdateTextFromThread(); while (true) { if (IsHandleCreated) { - if (encodeQueue.isEncoding || mainWin.isScanning) - updateTextFromThread(); + if (_encodeQueue.isEncoding || _mainWin.isScanning) + UpdateTextFromThread(); else { // The encode may just have stoped, so, refresh the log one more time before restarting it. - if (lastUpdate == false) - updateTextFromThread(); + if (_lastUpdate == false) + UpdateTextFromThread(); - 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 + _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(1000); @@ -131,14 +121,14 @@ namespace Handbrake } catch (Exception exc) { - MessageBox.Show("autoUpdate(): Exception: \n" + exc); + MessageBox.Show("AutoUpdate(): Exception: \n" + exc); } } - private void updateTextFromThread() + private void UpdateTextFromThread() { try { - String info = readFile(); + String info = ReadFile(); if (info.Contains("has exited")) info += "\n ############ End of Log ############## \n"; @@ -146,7 +136,7 @@ namespace Handbrake } catch (Exception exc) { - MessageBox.Show("updateTextFromThread(): Exception: \n" + exc); + MessageBox.Show("UpdateTextFromThread(): Exception: \n" + exc); } } private void SetText(string text) @@ -157,9 +147,10 @@ namespace Handbrake { if (rtf_actLog.InvokeRequired) { - IAsyncResult invoked = BeginInvoke(new SetTextCallback(SetText), new object[] {text}); + IAsyncResult invoked = BeginInvoke(new setTextCallback(SetText), new object[] { text }); EndInvoke(invoked); - } else + } + else rtf_actLog.AppendText(text); } } @@ -168,14 +159,15 @@ namespace Handbrake MessageBox.Show("SetText(): Exception: \n" + exc); } } - private String readFile() + private String ReadFile() { String appendText = String.Empty; try { // last_encode_log.txt 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), // we'll need to make a copy of it. - string logFile = Path.Combine(logDir, read_file); + string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; + string logFile = Path.Combine(logDir, _readFile); string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt"); // Make sure the application readable log file does not already exist. FileCopy fill fail if it does. @@ -183,18 +175,25 @@ namespace Handbrake File.Delete(logFile2); // Copy the log file. - File.Copy(logFile, logFile2); - - // Open the copied log file for reading + if (File.Exists(logFile)) + File.Copy(logFile, logFile2, true); + else + { + if (fileNotFoundQuickFix) + return ""; + fileNotFoundQuickFix = true; + return "\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; + } + StreamReader sr = new StreamReader(logFile2); string line; int i = 1; while ((line = sr.ReadLine()) != null) { - if (i > position) + if (i > _position) { appendText += line + Environment.NewLine; - position++; + _position++; } i++; } @@ -205,9 +204,8 @@ namespace Handbrake } catch (Exception exc) { - 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, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return "Error in ReadFile() \n Unable to read the log file.\n You may have to restart HandBrake. Will try reading the file again in a few seconds... \n Error Information: \n\n" + exc; } - return null; } protected override void OnClosing(CancelEventArgs e) @@ -243,11 +241,11 @@ namespace Handbrake } private void btn_scan_log_Click(object sender, EventArgs e) { - setLogView(true); + SetLogView(true); } private void btn_encode_log_Click(object sender, EventArgs e) { - setLogView(false); + SetLogView(false); } #endregion