Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Backend.Bootstrap
- {
- /// <summary>
- /// Occurs when the user requests to quit the application.
- /// </summary>
- public delegate void QuitApplicationEventHandler(object sender, ActionEventArgs e);
- /// <summary>
- /// Starts the bootstrap process.
- /// </summary>
- public delegate void StartBootstrapEventHandler();
- /// <summary>
- /// Defines the bootstrap view.
- /// </summary>
- public interface IBootstrapView : IView
- {
- /// <summary>
- /// Occurs when the user requests to quit the application.
- /// </summary>
- event QuitApplicationEventHandler QuitApplication;
- /// <summary>
- /// Starts the bootstrap process.
- /// </summary>
- event StartBootstrapEventHandler StartBootstrap;
- /// <summary>
- /// Title of the window.
- /// </summary>
- string Title { set; }
- /// <summary>
- /// Progress of bootstrap.
- /// </summary>
- ProgressInfo Progress { set; }
- }
- /// <summary>
- /// Defines the bootstrap presenter.
- /// </summary>
- public class BootstrapPresenter : Presenter<IBootstrapView>
- {
- /// <summary>
- /// Creates a new <see cref="BootstrapPresenter"/>.
- /// </summary>
- public BootstrapPresenter(IBootstrapView view)
- : base(view)
- {
- view.Title = "[KdG ExamenTool] v3.2.0 | Bootstrap";
- view.QuitApplication += QuitApplication;
- view.StartBootstrap += StartBootstrap;
- }
- #region Common
- /// <summary>
- /// Random object.
- /// </summary>
- private Random _random = new Random();
- /// <summary>
- /// Helper method for progress reporting.
- /// </summary>
- private void ReportProgress(ref BackgroundWorker backgroundWorker, int percentage = 0, ProgressInfo progressInfo = null)
- {
- if (backgroundWorker.WorkerReportsProgress) {
- backgroundWorker.ReportProgress(percentage, progressInfo);
- }
- }
- #endregion
- #region Bootstrap Process
- /// <summary>
- /// Holds a reference to the bootstrap background worker.
- /// </summary>
- private BackgroundWorker _bootstrapWorker;
- /// <summary>
- /// Action to perform when a cancellation is catched.
- /// </summary>
- private Action _bootstrapCancelAction;
- /// <summary>
- /// Starts the bootstrap process.
- /// </summary>
- private void StartBootstrap()
- {
- if (_bootstrapWorker == null) {
- _bootstrapWorker = new BackgroundWorker();
- _bootstrapWorker.WorkerReportsProgress = true;
- _bootstrapWorker.WorkerSupportsCancellation = true;
- }
- if (_bootstrapWorker.IsBusy) {
- return;
- }
- _bootstrapWorker.DoWork += BootstrapWorkerTask;
- _bootstrapWorker.ProgressChanged += BootstrapWorkerReportProgress;
- _bootstrapWorker.RunWorkerCompleted += BootstrapWorkerTaskCompleted;
- _bootstrapWorker.RunWorkerAsync();
- }
- private void BootstrapWorkerTask(object sender, DoWorkEventArgs e)
- {
- ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten..." });
- Thread.Sleep(_random.Next(250, 1250));
- BootstrapWorkerCheckCancel();
- ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Controleren instellingen." });
- Thread.Sleep(_random.Next(250, 1250));
- BootstrapWorkerCheckCancel();
- ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Detecteren platform." });
- Thread.Sleep(_random.Next(250, 1250));
- BootstrapWorkerCheckCancel();
- ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Woordenboeken inlezen." });
- Thread.Sleep(_random.Next(250, 1250));
- BootstrapWorkerCheckCancel();
- ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Locaties registreren." });
- Thread.Sleep(_random.Next(250, 1250));
- BootstrapWorkerCheckCancel();
- ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Interfaces opbouwen." });
- Thread.Sleep(_random.Next(250, 1250));
- BootstrapWorkerCheckCancel();
- }
- private void BootstrapWorkerReportProgress(object sender, ProgressChangedEventArgs e)
- {
- if (e.UserState != null) {
- View.Progress = e.UserState as ProgressInfo;
- }
- }
- private void BootstrapWorkerTaskCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- BootstrapWorkerCheckCancel();
- View.Progress = new ProgressInfo { IsIndeterminate = false, Message = "Opstarten voltooid.", Progress = 100, Range = new Int32Range(0, 100) };
- _bootstrapWorker.Dispose();
- _bootstrapWorker = null;
- BootstrapWorkerCheckCancel();
- }
- /// <summary>
- /// Checks whether a cancellation is pending and performs the cancellation action, if provided.
- /// </summary>
- private void BootstrapWorkerCheckCancel()
- {
- if (_bootstrapWorker == null) {
- if (_bootstrapCancelAction != null) {
- _bootstrapCancelAction();
- }
- return;
- }
- if (_bootstrapWorker.CancellationPending) {
- if (_bootstrapCancelAction != null) {
- _bootstrapCancelAction();
- }
- }
- }
- #endregion
- #region Quit Application
- /// <summary>
- /// Quits the application.
- /// </summary>
- private void QuitApplication(object sender, ActionEventArgs e)
- {
- if (e.HasAction) {
- _bootstrapCancelAction = () => {
- e.Action ();
- System.Environment.Exit (0);
- };
- } else {
- _bootstrapCancelAction = () => System.Environment.Exit (0);
- }
- if (_bootstrapWorker == null) {
- _bootstrapCancelAction ();
- } else {
- _bootstrapWorker.CancelAsync();
- }
- }
- #endregion
- }
- /// <summary>
- /// Information on progress.
- /// </summary>
- public class ProgressInfo
- {
- /// <summary>
- /// Gets/sets the progress message.
- /// </summary>
- public string Message { get; set; }
- /// <summary>
- /// Determines whether indeterminate or not.
- /// </summary>
- public bool IsIndeterminate { get; set; }
- /// <summary>
- /// Progress range.
- /// Ususally in percentage, 0 - 100.
- /// </summary>
- public IRange<int> Range { get; set; }
- /// <summary>
- /// The current progress.
- /// </summary>
- public int Progress { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement