OSDN Git Service

デットロックする可能性がある個所を修正した
[completeeraser/CompleteEraser.git] / CompleteEraser / MainForm.cs
1 using System;\r
2 using System.IO;\r
3 using System.Text;\r
4 using System.Threading;\r
5 using System.Threading.Tasks;\r
6 using System.Collections.Generic;\r
7 using System.Windows.Forms;\r
8 using System.Diagnostics;\r
9 using System.Linq;\r
10 using CompleteEraser.Properties;\r
11 \r
12 namespace CompleteEraser\r
13 {\r
14     public partial class MainForm : Form\r
15     {\r
16         CancellationTokenSource tokenSource;\r
17         Task task;\r
18         SharedInfo info;\r
19         IEnumerable<string> files;\r
20         IOperation op;\r
21 \r
22         public MainForm()\r
23         {\r
24             InitializeComponent();\r
25         }\r
26 \r
27         public MainForm(string[] args) : this()\r
28         {\r
29             if (args.Length == 0)\r
30             {\r
31                 try\r
32                 {\r
33                     this.info = new SharedInfo();\r
34                     this.files = this.info;\r
35                     this.op = new ShrredFiles();\r
36                 }\r
37                 catch (IOException)\r
38                 {\r
39                     MessageBox.Show("ファイルかフォルダーを指定してください");\r
40                     return;\r
41                 }\r
42             }\r
43             else if (args[0] == "/recylebin")\r
44             {\r
45                 this.op = new BreakRecyleBin();\r
46                 this.files = new RecyleBinCollection();\r
47             }else{\r
48                 this.op = new ShrredFiles();\r
49                 this.files = args;\r
50             }\r
51             this.progressBar1.Maximum = this.files.Count();\r
52             this.op.Progressing += new ProgressingEventHandler(op_Progressing);\r
53             this.op.Progressed += new ProgressedEventHandler(op_Progressed);\r
54         }\r
55 \r
56         private void MainForm_Shown(object sender, EventArgs e)\r
57         {\r
58             if (op == null)\r
59             {\r
60                 this.Close();\r
61                 return;\r
62             }\r
63 \r
64             this.tokenSource = new CancellationTokenSource();\r
65 \r
66             this.task = new Task(() =>\r
67             {\r
68                 op.Execute(this.files);\r
69             });\r
70             this.task.ContinueWith((t) =>\r
71             {\r
72                 this.BeginInvoke(new Action(() =>\r
73                 {\r
74                     this.Close();\r
75                 }));\r
76             });\r
77             this.task.Start();\r
78         }\r
79 \r
80         void  op_Progressing(object sender, ProgressingEventArgs e)\r
81         {\r
82             this.BeginInvoke(new Action(() =>\r
83             {\r
84                 if (e.fileName == null)\r
85                     this.label2.Text = Resources.FINAL_PROCESS;\r
86                 else\r
87                     this.label2.Text = string.Format(Resources.FILE_PROCESS, this.TrimFullPath(e.fileName));\r
88             }));\r
89         }\r
90 \r
91         void op_Progressed(object sender, ProgressedEventArgs e)\r
92         {\r
93             CancellationToken token = this.tokenSource.Token;\r
94             if (token.IsCancellationRequested)\r
95                 token.ThrowIfCancellationRequested();\r
96 \r
97             if (e.ex == null)\r
98             {\r
99                 this.BeginInvoke(new Action(() => { if (this.progressBar1.Value < this.progressBar1.Maximum) this.progressBar1.Value++; }));\r
100             }else if(e.ex is IOException){\r
101                 DialogResult result = MessageBox.Show(e.ex.Message, "", MessageBoxButtons.AbortRetryIgnore);\r
102                 switch (result)\r
103                 {\r
104                     case System.Windows.Forms.DialogResult.Abort:\r
105                         e.breaked = true;\r
106                         break;\r
107                     case System.Windows.Forms.DialogResult.Retry:\r
108                         e.retry = true;\r
109                         break;\r
110                 }\r
111             }\r
112             else if (e.ex is UnauthorizedAccessException)\r
113             {\r
114                 if (MessageBox.Show(string.Format(Resources.CONFIRMRUNAS, e.fileName), "", MessageBoxButtons.YesNo) == DialogResult.Yes)\r
115                 {\r
116                     ProcessStartInfo info = new ProcessStartInfo(Application.ExecutablePath);\r
117                     info.Verb = "runas";\r
118                     info.UseShellExecute = true;\r
119                     Process.Start(info);\r
120                     this.Hide();\r
121                     Thread.Sleep(1000);\r
122                     e.breaked = true;\r
123                 }\r
124             }\r
125             else\r
126             {\r
127                 MessageBox.Show(e.ex.Message);\r
128                 e.breaked = true;\r
129             }\r
130         }\r
131 \r
132         private void button1_Click(object sender, EventArgs e)\r
133         {\r
134             tokenSource.Cancel();\r
135         }\r
136 \r
137         private void MainForm_FormClosed(object sender, FormClosedEventArgs e)\r
138         {\r
139             if(tokenSource != null)\r
140                 tokenSource.Cancel();\r
141             if (this.info != null)\r
142                 this.info.Dispose();\r
143         }\r
144 \r
145         public string TrimFullPath(string filepath)\r
146         {\r
147             if (filepath == null || filepath == "")\r
148                 return string.Empty;\r
149             string DirectoryPart = Path.GetDirectoryName(filepath);\r
150             string FilenamePart = Path.GetFileName(filepath);\r
151             string[] slice = DirectoryPart.Split('\\');\r
152             if (slice.Length > 3)\r
153             {\r
154                 DirectoryPart = slice[0] + "\\..\\" + slice[slice.Length - 1];\r
155                 return DirectoryPart + "\\" + FilenamePart;\r
156             }\r
157             else\r
158                 return filepath;\r
159         }\r
160 \r
161     }\r
162 }\r