Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DebugEngine/3.3/DebugEngineView.cs @ 5365

Last change on this file since 5365 was 5222, checked in by epitzer, 14 years ago

Use a background worker to execute single steps to prevent the GUI locking up for long-running operations (#47)

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Windows.Forms;
24using HeuristicLab.Core;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using System.ComponentModel;
28namespace HeuristicLab.DebugEngine {
29
30  /// <summary>
31  /// Engine espcially for debugging
32  /// </summary>
33  [View("DebugEngine View")]
34  [Content(typeof(DebugEngine), true)]
35  public partial class DebugEngineView : ItemView {
36
37    #region Basics
38
39    /// <summary>
40    /// Gets or sets the current engine.
41    /// </summary>
42    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
43    public new DebugEngine Content {
44      get { return (DebugEngine)base.Content; }
45      set { base.Content = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of <see cref="DebugEngineView"/>.
50    /// </summary>
51    public DebugEngineView() {
52      InitializeComponent();
53    }
54
55    /// <summary>
56    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
57    /// </summary>
58    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
59    protected override void DeregisterContentEvents() {
60      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
61      base.DeregisterContentEvents();
62    }
63
64    /// <summary>
65    /// Adds event handlers to the underlying <see cref="IEngine"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
68    protected override void RegisterContentEvents() {
69      base.RegisterContentEvents();
70      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
71    }
72
73    /// <summary>
74    /// Updates all controls with the latest data of the model.
75    /// </summary>
76    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        executionStackView.Content = null;
81        operatorTraceView.Content = null;
82        operationContentView.Content = null;
83      } else {
84        executionStackView.Content = Content.ExecutionStack;
85        operatorTraceView.Content = Content.OperatorTrace;
86        operationContentView.Content = new OperationContent(Content.CurrentOperation);
87      }
88    }
89
90    protected override void SetEnabledStateOfControls() {
91      base.SetEnabledStateOfControls();
92      if (Content == null) {
93        stepButton.Enabled = false;
94        refreshButton.Enabled = false;
95      } else {
96        stepButton.Enabled = Content.CanContinue && Content.ExecutionState != ExecutionState.Started;
97        refreshButton.Enabled = Content.CurrentAtomicOperation != null && Content.ExecutionState != ExecutionState.Started;
98      }
99    }
100
101    #endregion
102
103    void Content_ExecutionStateChanged(object sender, EventArgs e) {
104      if (InvokeRequired) {
105        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
106      } else {
107        SetEnabledStateOfControls();
108        switch (Content.ExecutionState) {
109          case ExecutionState.Started:
110            if (!stepping) {
111              executionStackView.Content = null;
112              operatorTraceView.Content = null;
113              operationContentView.Content = null;
114            }
115            break;
116          default:
117            executionStackView.Content = Content.ExecutionStack;
118            operatorTraceView.Content = Content.OperatorTrace;
119            operationContentView.Content = new OperationContent(Content.CurrentOperation);
120            break;
121        }
122      }
123    }
124
125    private bool stepping = false;
126    private void stepButton_Click(object sender, EventArgs e) {
127      BackgroundWorker worker = new BackgroundWorker();
128      bool skipStackops = skipStackOpsCheckBox.Checked;
129      worker.DoWork += (s, a) => {
130        Content.Step(skipStackops);
131      };
132      worker.RunWorkerCompleted += (s, a) => {
133        stepping = false;
134        SetEnabledStateOfControls();
135      };
136      stepping = true;
137      stepButton.Enabled = false;
138      worker.RunWorkerAsync();
139    }
140
141    private void refreshButton_Click(object sender, EventArgs e) {
142      var content = Content;
143      Content = null;
144      Content = content;
145    }
146
147  }
148}
Note: See TracBrowser for help on using the repository browser.