Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation.Views/3.3/EvaluationCacheView.cs @ 6409

Last change on this file since 6409 was 6291, checked in by epitzer, 14 years ago

fixed synchronization issue in evaluation cache and simplified event handling (#1516)

File size: 2.4 KB
Line 
1using System;
2using System.Windows.Forms;
3using HeuristicLab.Core.Views;
4using HeuristicLab.MainForm;
5using System.Threading;
6using System.IO;
7using System.ComponentModel;
8
9namespace HeuristicLab.Problems.ExternalEvaluation.Views {
10
11  [View("EvaluationCacheView")]
12  [Content(typeof(EvaluationCache), IsDefaultView = true)]
13  public sealed partial class EvaluationCacheView : ParameterizedNamedItemView {
14
15    public new EvaluationCache Content {
16      get { return (EvaluationCache)base.Content; }
17      set { base.Content = value; }
18    }
19
20    public EvaluationCacheView() {
21      InitializeComponent();
22    }
23
24    protected override void DeregisterContentEvents() {
25      Content.Changed -= new System.EventHandler(Content_StatusChanged);
26      base.DeregisterContentEvents();
27    }
28
29    protected override void RegisterContentEvents() {
30      base.RegisterContentEvents();
31      Content.Changed += new System.EventHandler(Content_StatusChanged);
32    }
33
34    #region Event Handlers (Content)
35    void Content_StatusChanged(object sender, EventArgs e) {
36      if (InvokeRequired)
37        Invoke(new EventHandler(Content_StatusChanged), sender, e);
38      else
39        hits_sizeTextBox.Text = string.Format("{0}/{1} ({2} active)", Content.Hits, Content.Size, Content.ActiveEvaluations);
40    }
41
42    #endregion
43
44    protected override void OnContentChanged() {
45      base.OnContentChanged();
46      if (Content == null) {
47        hits_sizeTextBox.Text = "#/#";
48      } else {
49        Content_StatusChanged(this, EventArgs.Empty);
50      }
51    }
52
53    protected override void SetEnabledStateOfControls() {
54      base.SetEnabledStateOfControls();
55      clearButton.Enabled = !ReadOnly && Content != null;
56      saveButton.Enabled = !ReadOnly && Content != null;
57    }
58
59    #region Event Handlers (child controls)
60    private void clearButton_Click(object sender, EventArgs e) {
61      Content.Reset();
62    }
63    #endregion
64   
65    private void saveButton_Click(object sender, EventArgs e) {
66      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
67        saveButton.Enabled = false;
68        BackgroundWorker worker = new BackgroundWorker();
69        worker.DoWork += (s, a) => {
70          Content.Save((string)a.Argument);
71        };
72        worker.RunWorkerCompleted += (s, a) => {
73          SetEnabledStateOfControls();
74        };
75        worker.RunWorkerAsync(saveFileDialog.FileName);
76      }
77    }   
78  }
79}
Note: See TracBrowser for help on using the repository browser.