source: branches/eraser6/6.0/Eraser/ProgressForm.cs @ 1659

Revision 1659, 5.0 KB checked in by lowjoel, 5 years ago (diff)

When dealing with the LogForm? and the ProgressForm? classes, for events in which we wait for triggering by the task object, we should check that the form we belong to exists (i.e. not disposed) before we do anything on the form since forms which have Disposed() called will still receive events. There is a fix removed in this revision (in ProgressForm?.cs/WrapItemName) which may introduce new bugs but since this is really hard to reproduce I'll commit a blind fix and wait for more reports to come in.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2009 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29
30using Eraser.Manager;
31using Eraser.Util;
32using System.Globalization;
33
34namespace Eraser
35{
36    public partial class ProgressForm : Form
37    {
38        private Task task;
39        private DateTime lastUpdate;
40
41        public ProgressForm(Task task)
42        {
43            InitializeComponent();
44            UXThemeApi.UpdateControlTheme(this);
45            this.task = task;
46            this.lastUpdate = DateTime.Now;
47            this.ActiveControl = hide;
48
49            //Register the event handlers
50            jobTitle.Text = task.UIText;
51            task.ProgressChanged += task_ProgressChanged;
52            task.TaskFinished += task_TaskFinished;
53        }
54
55        private void ProgressForm_FormClosed(object sender, FormClosedEventArgs e)
56        {
57            task.ProgressChanged -= task_ProgressChanged;
58            task.TaskFinished -= task_TaskFinished;
59        }
60
61        private void task_ProgressChanged(object sender, TaskProgressEventArgs e)
62        {
63            if (!IsHandleCreated)
64                return;
65            if (InvokeRequired)
66            {
67                //Don't update too often - we can slow down the code.
68                if (DateTime.Now - lastUpdate < new TimeSpan(0, 0, 0, 0, 300))
69                    return;
70
71                lastUpdate = DateTime.Now;
72                Invoke(new EventHandler<TaskProgressEventArgs>(task_ProgressChanged), sender, e);
73                return;
74            }
75
76            status.Text = e.CurrentTargetStatus;
77            item.Text = WrapItemName(e.CurrentItemName);
78            pass.Text = e.CurrentTargetTotalPasses != 0 ?
79                S._("{0} out of {1}", e.CurrentItemPass, e.CurrentTargetTotalPasses) :
80                e.CurrentItemPass.ToString(CultureInfo.CurrentCulture);
81
82            if (e.TimeLeft >= TimeSpan.Zero)
83                timeLeft.Text = S._("About {0:T} left", e.TimeLeft);
84            else
85                timeLeft.Text = S._("Unknown");
86
87            if (e.CurrentItemProgress >= 0.0f)
88            {
89                itemProgress.Style = ProgressBarStyle.Continuous;
90                itemProgress.Value = (int)(e.CurrentItemProgress * 1000);
91                itemProgressLbl.Text = e.CurrentItemProgress.ToString("#0%",
92                    CultureInfo.CurrentCulture);
93            }
94            else
95            {
96                itemProgress.Style = ProgressBarStyle.Marquee;
97                itemProgressLbl.Text = string.Empty;
98            }
99
100            overallProgress.Value = (int)(e.OverallProgress * 1000);
101            overallProgressLbl.Text = S._("Total: {0,2:#0.00%}", e.OverallProgress);
102        }
103
104        private void task_TaskFinished(object sender, TaskEventArgs e)
105        {
106            if (!IsHandleCreated)
107                return;
108            if (InvokeRequired)
109            {
110                Invoke(new EventHandler<TaskEventArgs>(task_TaskFinished), sender, e);
111                return;
112            }
113
114            //Update the UI. Set everything to 100%
115            timeLeft.Text = item.Text = pass.Text = string.Empty;
116            overallProgressLbl.Text = S._("Total: {0,2:#0.00%}", 1.0);
117            overallProgress.Value = overallProgress.Maximum;
118            itemProgressLbl.Text = "100%";
119            itemProgress.Style = ProgressBarStyle.Continuous;
120            itemProgress.Value = itemProgress.Maximum;
121
122            //Inform the user on the status of the task.
123            LogLevel highestLevel = LogLevel.Information;
124            LogEntryCollection entries = e.Task.Log.LastSessionEntries;
125            foreach (LogEntry log in entries)
126                if (log.Level > highestLevel)
127                    highestLevel = log.Level;
128
129            switch (highestLevel)
130            {
131                case LogLevel.Warning:
132                    status.Text = S._("Completed with warnings");
133                    break;
134                case LogLevel.Error:
135                    status.Text = S._("Completed with errors");
136                    break;
137                case LogLevel.Fatal:
138                    status.Text = S._("Not completed");
139                    break;
140                default:
141                    status.Text = S._("Completed");
142                    break;
143            }
144
145            //Change the Stop button to be a Close button and the Hide button
146            //to be disabled
147            hide.Enabled = false;
148            stop.Text = S._("Close");
149        }
150
151        private void hide_Click(object sender, EventArgs e)
152        {
153            Close();
154        }
155
156        private void stop_Click(object sender, EventArgs e)
157        {
158            if (task.Executing)
159                task.Cancel();
160            Close();
161        }
162
163        private string WrapItemName(string itemName)
164        {
165            StringBuilder result = new StringBuilder(itemName.Length);
166            using (Graphics g = item.CreateGraphics())
167            {
168                //Split the long file name into lines which fit into the width of the label
169                while (itemName.Length > 0)
170                {
171                    int chars = 0;
172                    int lines = 0;
173                    g.MeasureString(itemName, item.Font, new SizeF(item.Width - 2, 15),
174                        StringFormat.GenericDefault, out chars, out lines);
175
176                    result.AppendLine(itemName.Substring(0, chars));
177                    itemName = itemName.Remove(0, chars);
178                }
179            }
180
181            return result.ToString();
182        }
183    }
184}
Note: See TracBrowser for help on using the repository browser.