OSDN Git Service

ebee0ea4258226208385c6bc28469886873b7885
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmActivityWindow.cs
1 /*  frmActivityWindow.cs $\r
2         \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
6 \r
7 using System;\r
8 using System.Collections.Generic;\r
9 using System.ComponentModel;\r
10 using System.Data;\r
11 using System.Drawing;\r
12 using System.Text;\r
13 using System.Windows.Forms;\r
14 using System.IO;\r
15 using System.Threading;\r
16 \r
17 \r
18 namespace Handbrake\r
19 {\r
20     public partial class frmActivityWindow : Form\r
21     {\r
22         /// <summary>\r
23         /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.\r
24         /// </summary>\r
25         /// \r
26         Thread monitorFile;\r
27         String read_file;\r
28         public frmActivityWindow(string file)\r
29         {\r
30             InitializeComponent();\r
31             this.rtf_actLog.Text = string.Empty;\r
32             monitorFile = new Thread(autoUpdate);\r
33             read_file = file;\r
34 \r
35         }\r
36 \r
37         private void frmActivityWindow_Load(object sender, EventArgs e)\r
38         {\r
39             this.rtf_actLog.Text = string.Empty;\r
40             rtf_actLog.Text = readFile();\r
41    \r
42             monitorFile.Start();\r
43         }\r
44 \r
45         private void autoUpdate(object state)\r
46         {\r
47             while (true)\r
48             {\r
49                 updateTextFromThread();\r
50                 Thread.Sleep(3000);\r
51             }\r
52         }\r
53         \r
54         private delegate void UpdateUIHandler();\r
55         private void updateTextFromThread()\r
56         {\r
57             if (this.InvokeRequired)\r
58             {\r
59                 this.BeginInvoke(new UpdateUIHandler(updateTextFromThread));\r
60                 return;\r
61             }\r
62             rtf_actLog.Text = readFile();\r
63             this.rtf_actLog.SelectionStart = this.rtf_actLog.Text.Length -1;\r
64             this.rtf_actLog.ScrollToCaret();\r
65 \r
66             if (rtf_actLog.Text.Contains("HandBrake has exited."))\r
67                 monitorFile.Abort();\r
68         }\r
69 \r
70         private string readFile()\r
71         {\r
72             string log = "";\r
73             try\r
74             {\r
75                 // hb_encode_log.dat is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it,\r
76                 // we'll need to make a copy of it.\r
77                 string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
78                 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\r
79 \r
80                 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
81                 if (File.Exists(logFile2))\r
82                     File.Delete(logFile2);\r
83 \r
84                 // Copy the log file.\r
85                 File.Copy(logFile, logFile2);\r
86 \r
87                 // Begin processing the log file.\r
88                 StreamReader sr = new StreamReader(logFile2);\r
89                 string line = sr.ReadLine();\r
90                 while (line != null)\r
91                 {\r
92                     log = log + (line + System.Environment.NewLine);\r
93                     line = sr.ReadLine();\r
94                 }\r
95                 sr.Close();\r
96             }\r
97             catch (Exception exc)\r
98             {\r
99                 MessageBox.Show("An Error has occured! \n\n" + exc.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
100             }\r
101 \r
102             return log;\r
103         }\r
104     }\r
105 }