OSDN Git Service

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