/*
* DictionaryProgressForm.cs - Displays a progress bar while reading
* the dictionary of a Level 9 game.
*
* Copyright (C) 2004 - 2011 Andreas Scherrer
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
using System;
using System.Windows.Forms;
namespace Level9
{
///
/// Displays a progress bar while reading the dictionary of a Level 9 game.
///
public partial class DictionaryProgressForm : Form
{
// -- Constants ------------------------------------------------------
///
/// The maximum time for a dictionary import in milliseconds.
///
public const int WATCHDOG_TIMEOUT = 20000;
// -- Attributes -----------------------------------------------------
///
/// A watchdog timer to control the dictionary import.
///
System.Windows.Forms.Timer watchdogTimer =
new System.Windows.Forms.Timer();
///
/// A handler for OsFlush events raised by the interpreter.
///
OsFlushEventHandler osFlushHandler;
// -- Constructors ---------------------------------------------------
///
/// Initializes a new instance of the DictionaryProgressForm class.
///
public DictionaryProgressForm()
{
InitializeComponent();
progressBarRead.Value = progressBarRead.Minimum;
osFlushHandler = new OsFlushEventHandler(InterpreterOsFlush);
Interpreter.OsFlush += osFlushHandler;
watchdogTimer.Tick += new EventHandler(WatchdogTimerTick);
watchdogTimer.Interval = WATCHDOG_TIMEOUT;
}
// -- Event handling -------------------------------------------------
///
/// Invoked when the interpreter flushes a portion of text.
///
///
/// an OsFlushEventArgs that contains the event data
///
void InterpreterOsFlush(OsFlushEventArgs e)
{
if (this.InvokeRequired == false
&& e.Command == Interpreter.Commands.Dictionary) {
watchdogTimer.Start();
Dictionary.Instance.Add(e.Text);
if (progressBarRead.Value == progressBarRead.Maximum) {
progressBarRead.Value = progressBarRead.Minimum;
}
progressBarRead.PerformStep();
if (e.Text.EndsWith("\0")) {
Interpreter.OsFlush -= osFlushHandler;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
else {
OsFlushEventHandler osFlushHandler =
new OsFlushEventHandler(InterpreterOsFlush);
this.Invoke(osFlushHandler, new object[] { e });
}
}
///
/// Invoked when a watchdog timer interval has expired.
///
/// the object that originated the event
/// an EventArgs that contains the event data
void WatchdogTimerTick(Object sender, EventArgs e)
{
Interpreter.OsFlush -= osFlushHandler;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}